【发布时间】:2010-08-31 23:08:10
【问题描述】:
我有两个用户控件(uc1 和 uc2)加载到第三个用户控件(shell)中。 Shell 有两个属性 uc1 和 uc2,类型为 UserControl1 和 UserControl2,并且每个属性都有一个 DependencyProperty 注册到它们自己的名为 IsDirty 的类:
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register("IsDirty", typeof (bool), typeof (UserControl1));
public bool IsDirty
{
get { return (bool) GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
(UserControl2 的代码相同)
Shell 将 TextBlocks 绑定到 IsDirty 属性:
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
<TextBlock Text="{Binding ElementName=shell, Path=Uc2.IsDirty}"/>
当我在 uc1 和 uc2 中更改 IsDirty 的值时,Shell 永远不会收到通知。我错过了什么? UserControl 是 DependencyObject 的后代...
如果我有通过 INotifyPropertyChanged 通知更改的常规属性,则会发生相同的行为。
如果我从 uc1 和 uc2 引发路由事件,冒泡到 Shell,那么我可以捕获 Dirty 值并且一切正常,但我不应该这样做,不是吗?
谢谢
编辑:答案是在 Uc1 和 Uc2 属性上引发属性更改事件或将它们设为 DP。
【问题讨论】:
-
您的绑定语句假定 Shell 在其中具有两个属性:“Uc1”和“Uc2”。它是否正确?还是“Uc1”和“Uc2”只是在 Shell 的 XAML 中添加的 UserControl 的实例?
-
是的,这是正确的。 Shell 有两个属性:分别属于 UserControl1 和 UserControl2 类型的 uc1 和 uc2。我将使用此信息更新问题。
-
好的。还有一个问题,为什么上面的DependencyProperty的定义叫“SecondDirtyProperty”?这是正确的还是应该是“IsDirtyProperty”?
-
对不起,它是 IsDirtyProperty。我刚刚重命名了要在此处发布的属性:)
标签: wpf dependency-properties inotifypropertychanged