PropertyChanged没有开火

时间:2010-10-23 22:43:07

标签: winforms compact-framework inotifypropertychanged

我有一个奇怪的情况:

在.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属性的更改时评估此属性,但不是!!!

为什么这不起作用,我错过了什么?

提前致谢

1 个答案:

答案 0 :(得分: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