2010-10-23 75 views
0

我有一个奇怪的现象:的PropertyChanged没有发射

在.NET CF项目

还有一类(称为A),它具有以下结构:

public partial class A: Form, INotifyPropertyChanged 
{ 
//for simplicity stripping off everything unrelated to this problem 

     private int _SelectedRowsCount = 0; 
     public int SelectedRowsCount 
     { 
      get { return _SelectedRowsCount; } 
      set 
      { 
       _SelectedRowsCount = value; 
       OnPropertyChanged("SelectedRowsCount"); 
      } 
     } 

     public bool enableCollectionButton 
     { 
      get { return SelectedRowsCount > 0; } 
     } 

//.... 
// 
// 


     void SomeMethod() 
{ 
//for simplicity: 
SelectedRowsCount = 1; //<- HERE NOT FIRING Propertychanged for enableCollectionButton 
} 
} 

类正确地实现了INotifyPropertyChanged的接口,使SelectedRowsCount属性触发属性更改通知(我使用调试器对此进行了评估)。 的enableCollectionButton属性数据绑定到一定的控制,像这样:

someButton.DataBindings.Add("Enabled", this, "enableCollectionButton"); 

但enableCollectionButton属性不会更改(尽管这取决于SelectedRowsCount的值)。此属性应该在SelectedRowsCount属性的更改上进行评估,但不是!

为什么这不起作用,我怎么想?

在此先感谢

回答

0

试试这个

public partial class A: Form, INotifyPropertyChanged 
{ 
//for simplicity stripping off everything unrelated to this problem 

    private int _SelectedRowsCount = 0; 
    public int SelectedRowsCount 
    { 
     get { return _SelectedRowsCount; } 
     set 
     { 
      _SelectedRowsCount = value; 
      OnPropertyChanged("SelectedRowsCount"); 
      OnPropertyChanged("enableCollectionButton"); //This changes too ! 
     } 
    } 

    public bool enableCollectionButton 
    { 
     get { return SelectedRowsCount > 0; } 
    } 
} 

会发生什么事是你要绑定到enableCollectionButton财产,但你不通知的变化BindingManager的到enableCollectionButton,而的更改为SelectedRowsCount。 BindingManager不知道它们是相关的!

同时尝试使用Microsoft's naming conventionsenableCollectionButton应该是EnableCollectionButton

+0

非常感谢!我怎么能负责吗?!我应该去散步!!哈哈!是的,你是关于命名约定(通常我遵守约定)!非常感谢 – 2010-10-24 08:59:22

+0

不客气! – 2010-10-24 09:13:32