我有一个奇怪的情况:
在.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属性时求值,但实际不是!
为什么这个不起作用,我错过了什么??
提前感谢
发布于 2010-10-24 16:31:01
尝尝这个
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 conventions,enableCollectionButton应该是EnableCollectionButton
https://stackoverflow.com/questions/4006434
复制相似问题