2013-05-19 42 views
0

我无法理解发生在我身上的问题。何时触发PropertyChangedCallback

我有一个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> 

在我的理解中PropertyChangedCallbacked被称为每当DependencyProperty的值变化。

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

有没有其他的可能性,一旦房产被初始化或得到通知我,我只是失去了一些东西?

编辑: 也许我不清楚这一点。我的问题是,当初始值设置为SomeProperty时,PropertyCahngedCallback未被触发。

+0

你到底想干什么? – mydogisbox

+0

我编辑了这个问题。当初始值设置为_SomeProperty_时,我需要得到通知。 – Robert

回答

0

对不起,我没有仔细看过日志。 问题是,事件我没有解雇,但事实上,绑定我可能。 似乎与接口类型的Windows 8商店应用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/

相关问题