Silverlight Custom User Controls and Dependancy property Issue
我有一个名为Field:
的自定义用户控件
1 2 3 4 5 6 7 8 9 10 11 | <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Name="LblName"/> <TextBox Grid.Row="0" Grid.Column="1" HorizontalContentAlignment="Left" Name="TxtValue"/> </Grid> |
在CS文件中,我公开了以下属性,该属性仅设置文本框的" .text"值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(string), typeof(Field), new PropertyMetadata( default(string), (source, args) => (source as Field).TxtValue.Text = (string)args.NewValue ) ); [Category("Custom")] public string Value { get { return (string)this.GetValue(ValueProperty); } set { this.SetValue(ValueProperty, value); } } |
第一个问题。这是传播子用户控件依赖项属性(通过父用户控件进行备份,以便可以从xaml页进行设置)的正确方法吗?我在网上环顾四周,考虑到在编写自定义控件时这是一件很原始的事情,因此在这里并没有太多内容。
在更大的范围内提供详细信息,我有客户详细信息页面,一些控件绑定到包含在可观察集合中的客户对象。如果我将页面上的文本框(包含在"字段"控件中)的值从" xxxx"更改为" yyyy",我将始终返回" xxxx"。吸气剂周围某处好像出了什么问题?
这绝对是允许属性传播的正确方向。
如果这是一个真正的自定义控件(来自Control的派生),则您想使用
如果这是一个UserControl,则可以使用Binding,但确实有一些其他选择。有了它,您的属性更改后的回调函数只会朝一个方向:您只设置了Text值,就不会对其做出反应。
希望那里的文档有所改进。