简体   繁体   English

XamlWriter不会序列化WPF CustomControl的属性

[英]Property of WPF CustomControl is not serialized by XamlWriter

We have made a WPF CustomControl which inherits from the TextBox and added a new DependencyProperty "TableName", so we can set there the name of the DataTable to which the control is bound. 我们制作了一个WPF CustomControl,它继承自TextBox,并添加了一个新的DependencyProperty“ TableName”,因此我们可以在其中设置控件绑定到的DataTable的名称。 The control is generated at runtime and the property TableName pe set to "table1". 该控件在运行时生成,并且属性TableName pe设置为“ table1”。 Then we serialize the Panel which contains the controls (like our CustomControl) to a XAML file like in this article from CodeProject, because we want to serialize the bindings too: 然后,我们将包含控件的Panel(如CustomControl)序列化为XAML文件,如本文中的CodeProject,因为我们也希望序列化绑定:

XamlWriter-and-Bindings-Serialization XamlWriter-and-Bindings-Serialization

Unfortunately - instead of properties like Width - our new DependencyProperty is not serialized. 不幸的是,我们没有将新的DependencyProperty而不是Width等属性进行序列化。

Is there a way to force the XamlWriter to serialize the property? 有没有办法强制XamlWriter序列化属性?

Thanks in advance! 提前致谢!

This is the Definition of the property in the CustomControl called "CustomTextBox": 这是CustomControl中称为“ CustomTextBox”的属性的定义:

public static readonly DependencyProperty TableNameProperty = DependencyProperty.Register("TableName", typeof(string), typeof(CustomTextBox));

public string TableName { get; set; }

It's style in the Generic.xaml: Generic.xaml中的样式:

<Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
</Style>

You have provided a CLR property but it doesn't do anything. 您提供了CLR属性,但它没有任何作用。 You need to link it to your DependencyProperty: 您需要将其链接到您的DependencyProperty:

public string TableName
{
    get { return (string)GetValue(TableNameProperty); }
    set { SetValue(TableNameProperty, value); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM