首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PropertyChanged未触发

PropertyChanged未触发
EN

Stack Overflow用户
提问于 2010-10-24 06:43:07
回答 1查看 582关注 0票数 0

我有一个奇怪的情况:

在.NET CF项目中,有一个类(称为A),其结构如下:

代码语言:javascript
复制
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属性被数据绑定到一些控件,如下所示:

代码语言:javascript
复制
someButton.DataBindings.Add("Enabled", this, "enableCollectionButton");

但是enableCollectionButton属性不会改变(尽管这取决于SelectedRowsCount的值)。此属性应在更改SelectedRowsCount属性时求值,但实际不是!

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

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-24 16:31:01

尝尝这个

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4006434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档