简体   繁体   English

PropertyChanged不触发

[英]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: 在.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
}
}

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). 该类正确实现了INotifyPropertyChanged接口,该接口使SelectedRowsCount属性能够触发属性更改的通知(我使用调试器对此进行了评估)。 The enableCollectionButton property is databound to some control like so: enableCollectionButton属性已绑定到某些控件,如下所示:

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

But the enableCollectionButton property does not change (though depending on the value of SelectedRowsCount). 但是enableCollectionButton属性不会更改(尽管取决于SelectedRowsCount的值)。 This property should be evaluated on a change of the SelectedRowsCount property, BUT IS NOT!!! 应该在SelectedRowsCount属性的更改上评估此属性,但不能!

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 . 发生的事情是您绑定到enableCollectionButton属性,但没有通知BindingManager对enableCollectionButton的更改,而不是对SelectedRowsCount的更改。 The BindingManager doesn't know they're related! BindingManager不知道它们之间的关系!

Also try using Microsoft's naming conventions , enableCollectionButton should be EnableCollectionButton 也请尝试使用Microsoft的命名约定enableCollectionButton应该为EnableCollectionButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM