PropertyChanged 未触发

发布于 2024-09-29 12:18:45 字数 1164 浏览 4 评论 0原文

我遇到了一个奇怪的情况:

在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-10-06 12:18:46

尝试一下,

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 的更改,而不是对 >选定的行计数。 BindingManager 不知道它们是相关的!

另请尝试使用 Microsoft 的命名约定enableCollectionButton应该是EnableCollectionButton

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文