PropertyChanged not firing
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
{
开发者_StackOverflow 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
Try this
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; }
}
}
What happens is that you're binding to the enableCollectionButton
property, but you're not notifying the BindingManager of the change to enableCollectionButton
, rather of the change to SelectedRowsCount
. The BindingManager doesn't know they're related!
Also try using Microsoft's naming conventions, enableCollectionButton
should be EnableCollectionButton
精彩评论