Mat*_*mes 7 data-binding wpf dependency-properties
我们有一个派生自DependencyObject的对象,并实现了一些DependencyProperties.
基本上是这样的:
class Context : DependencyObject {
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register ("Name", typeof (string), typeof (Context), new PropertyMetadata (""));
public string Name {
get {
return (string)this.GetValue (NameProperty);
}
set {
this.SetValue (NameProperty, value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,属性设置,可以绑定等.当我使用TwoWay绑定从WPF绑定到属性时问题出现了.TwoWay部分从未实际发生过,WPF从不调用此属性的集合.我已经设置了这样的绑定:
<TextBox Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,键入文本框应立即更新Name属性,但不会.如果我将Name属性更改为常规POCO属性,它就可以工作(尽管TwoWay的另一面显然不会,除非我实现了INotifyPropertyChanged).
我在这做错了什么?这应该是一件非常简单的事情,但它让我头疼不已.
Ken*_*art 11
这是预期的行为.CLR属性只是底层的包装DependencyProperty.WPF经常通过调用优化GetValue和SetValue直接.如果您需要执行自定义逻辑,那么使用的元数据DependencyProperty.