我无法理解发生在我身上的问题。
我有一个类似这样的结构的 UserControl。
public class SomePage : Page
{
public static readonly DependencyProperty SomePropertyProperty =
DependencyProperty.Register("SomeProperty", typeof(IPropertyValue), typeof(SomeControl), new PropertyMetadata(null, new PropertyChangedCallback(OnSomePropertyChanged)));
private static void OnSomePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Do Some Stuff
}
}
还有一个看起来像这样的 ViewModel
public class SomeViewModel : BindableBase
{
private IPropertyValue _prop;
public IPropertyValue Property
{
get
{
if (_prop== null)
_prop = new SomeConcreteValue();
return _prop;
}
}
}
整个东西都绑定到一个页面
<common:LayoutAwarePage>
<Page.DataContext>
<vm:SomeViewModel />
</Page.DataContext>
<ctrl:SomePage SomeProperty="{Binding Property}" />
</common:LayoutAwarePage>
据我了解,只要 DependencyProperty 的值发生变化,就会调用 PropertyChangedCallbacked。
尽管 ViewModel.Property 的值永远不会改变,但 DependencyProperty“SomeProperty”的值仍然会改变,因为它从 null 变为初始绑定值。
一旦属性被初始化,是否还有其他可能得到通知,或者我只是在这里遗漏了什么?
编辑:也许我不清楚这一点。我的问题是当初始值设置为 SomeProperty 时不会触发 PropertyCahngedCallback。