何时触发了PropertyChangedCallback

时间:2013-05-19 17:59:41

标签: wpf data-binding mvvm windows-8 dependency-properties

我无法理解发生在我身上的问题。

我有一个像这样结构化的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;
        }
    }
}

整个Stuff绑定到一个Page

<common:LayoutAwarePage>
    <Page.DataContext>
        <vm:SomeViewModel />
    </Page.DataContext>
    <ctrl:SomePage SomeProperty="{Binding Property}" />
</common:LayoutAwarePage>

根据我的理解,只要DependencyProperty的值发生变化,就会调用PropertyChangedCallbacked。

尽管ViewModel.Property的值永远不会改变,但DependencyProperty“SomeProperty”的值仍然存在,因为它从null变为初始边界值。

一旦房产初始化或者我在这里遗漏了什么,是否还有其他可能获得通知?

修改: 也许我对此并不清楚。我的问题是,当初始值设置为SomeProperty时,不会触发PropertyCahngedCallback。

1 个答案:

答案 0 :(得分:0)

对不起,我没有仔细查看日志。 问题是,事件我没有解雇,但事实上,绑定我不可能。 似乎在Windows 8 Store中,具有接口类型的DependencyProperties无法按预期工作:

Error: Converter failed to convert value of type '#Namespace.ConcretePropertyType#, #Application#, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' to type 'IPropertyValue'; BindingExpression: Path='Property' DataItem=''Namespace.SomeViewModel, #Application#, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is '#Namespace.ConcretePropertyType#' (Name='null'); target property is '#ConcretePropertyType#' (type 'IPropertyValue'). 

来源: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/5ec0a4ba-b80a-4a8a-8e5a-f2fe776c45b5/

相关问题