0

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

我有一个类似这样的结构的 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。

4

1 回答 1

0

抱歉,我没有仔细查看日志。问题不是事件没有被触发,但实际上绑定是不可能的。似乎在具有接口类型的 Windows 8 Store Apps 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/

于 2013-05-23T12:55:28.813 回答