【问题标题】:When is the PropertyChangedCallback fired什么时候触发 PropertyChangedCallback
【发布时间】:2013-05-19 17:59:40
【问题描述】:

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

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

【问题讨论】:

  • 你想做什么?
  • 我已经编辑了这个问题。当初始值设置为 SomeProperty 时,我需要得到通知。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2017-11-20
    • 2014-02-04
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多