我有一个基类和另一个从基类继承的类。基类有一个 DependencyProperty(比如“MyDPProperty”):
public int MyDPProperty
{
get { return (int)GetValue(MyDPPropertyProperty); }
set { SetValue(MyDPPropertyProperty, value); }
}
public static readonly DependencyProperty MyDPPropertyProperty =DependencyProperty.Register("MyDPProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));
在 Window 的构造函数中,我写道:
SomeWpfWindow.DataContext = new ChildClass();
在我的 Window 的 XAML 代码中,我有:
<TextBox x:Name="txt"
Text="{Binding Path=MyDPProperty, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
现在,绑定不起作用,尽管当我在它后面的代码中绑定它时:
SomeWpfWindow.txt.SetBinding(TextBox.TextProperty
, new Binding("MyDPProperty")
{
Source = InstanceOfChildClass,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Mode = BindingMode.TwoWay
});