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 属性的更改来评估此属性,但事实并非如此!
为什么这不起作用,我想念什么?
提前致谢
I have a strange situation:
in a .NET CF project there is a class (call it A) which has the following structure:
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
}
}
The class implements correctly the INotifyPropertyChanged interface which makes the the SelectedRowsCount property to fire a property changed notification (i evaluated this with the debugger).
The enableCollectionButton property is databound to some control like so:
someButton.DataBindings.Add("Enabled", this, "enableCollectionButton");
But the enableCollectionButton property does not change (though depending on the value of SelectedRowsCount). This property should be evaluated on a change of the SelectedRowsCount property, BUT IS NOT!!!
Why is this not functioning, what do i miss??
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下,
发生的情况是您绑定到
enableCollectionButton
属性,但您没有通知 BindingManager 对enableCollectionButton
的更改,而不是对>选定的行计数
。 BindingManager 不知道它们是相关的!另请尝试使用 Microsoft 的命名约定、
enableCollectionButton
应该是EnableCollectionButton
Try this
What happens is that you're binding to the
enableCollectionButton
property, but you're not notifying the BindingManager of the change toenableCollectionButton
, rather of the change toSelectedRowsCount
. The BindingManager doesn't know they're related!Also try using Microsoft's naming conventions,
enableCollectionButton
should beEnableCollectionButton