ObservableCollections 和 C# 中属性的更改

发布于 2024-12-12 17:44:33 字数 1377 浏览 2 评论 0原文

我正在使用我定义的 Job 类的可观察集合。我绑定了一个方法来处理 INotifyCollectionChanged 事件。 MSDN 告诉我,INotifyCollectionChanged 是“动态更改的侦听器,例如添加和删除项目或刷新整个列表时”,但我想侦听任何属性的更改集合中的作业类,是否有一个事件处理程序?我知道有一个 INotifyPropertyChanged 接口,但我希望它可以在集合上工作。

编辑:

说实话,我对此感到困惑,所以我应该为我正在做的事情提供更多背景信息,这样我才能得到答案。我在“作业”类中拥有此属性:

    public Boolean IsPlanned
    {
        get
        {
            return this.Storage<Job>().isPlanned;
        }
        set
        {
            var storage = this.Storage<Job>();

            if (storage.isPlanned != value)
            {
                storage.isPlanned = value;
                this.OnPropertyChanged(() => this.isPlanned);
                MessageBox.Show("IsPlanned property was changed on one of the jobs " + this.Subject);
            }
        }
    }

此作业类实际上继承自 telerik 控件的约会类(恰好实现了 INotifyPropertyChanged)。从 telerik 文档中我还得到了上面的代码(减去消息框)。现在,当我更改此布尔值一次时,该消息框行将被执行 5 次。

任何帮助表示赞赏!

编辑 2:IsPlanned 的路径已更改:

PresentationManager.Instance.AllJobs.Single(o => o.JobGuid.Equals(((Job)state.DraggedAppointments.First()).JobGuid)).IsPlanned = true;

PresentationManager.Instance.AllJobs.Single(o => o.JobGuid.Equals(((Job)payload.DraggedAppointments.First()).JobGuid)).IsPlanned = false;

这些都来自不同的类,用于定义我的自定义拖放行为的覆盖(来自列表框)。

I'm working with an observable collection of a Job class I have defined. I have binded a method to handle the INotifyCollectionChanged event. MSDN tells me that INotifyCollectionChanged is a "listener of dynamic changes, such as when items get added and removed or the whole list is refreshed," but I'd like to listen for changes to properties to any of the job classes in the collection, is there an event handler for this?? I understand there is an INotifyPropertyChanged interface but I want this to work on a collection.

EDIT:

I'm confused by this to be honest so I should give more background info to what I'm doing so I can get my answer. I have this property in a 'Job' class:

    public Boolean IsPlanned
    {
        get
        {
            return this.Storage<Job>().isPlanned;
        }
        set
        {
            var storage = this.Storage<Job>();

            if (storage.isPlanned != value)
            {
                storage.isPlanned = value;
                this.OnPropertyChanged(() => this.isPlanned);
                MessageBox.Show("IsPlanned property was changed on one of the jobs " + this.Subject);
            }
        }
    }

This job class actually inherits from a telerik control's appointment class (which just so happens to implement INotifyPropertyChanged). From telerik documentation I also got the above code (minus the messagebox). Now when I'm changing this boolean ONCE, that message box line is bein executed 5 times.

Any help appreciated!!

EDIT 2: Paths were IsPlanned is changed:

PresentationManager.Instance.AllJobs.Single(o => o.JobGuid.Equals(((Job)state.DraggedAppointments.First()).JobGuid)).IsPlanned = true;

PresentationManager.Instance.AllJobs.Single(o => o.JobGuid.Equals(((Job)payload.DraggedAppointments.First()).JobGuid)).IsPlanned = false;

These are both from different classes that are used to define override's for my custom drag drop behaviour (from listbox).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深空失忆 2024-12-19 17:44:34

在您的 Job< 上实现 INotifyPropertyChanged 接口< /代码> 类。然后,您应该可以在 ObservableCollection 上使用 PropertyChanged

完全支持从绑定源对象传输数据值
绑定目标,集合中支持的每个对象
可绑定属性必须实现适当的更改属性
通知机制,例如 INotifyPropertyChanged 接口。

Implement the INotifyPropertyChanged interface on your Job class. This should then allow you to use the PropertyChanged on your ObservableCollection<Job>.

To fully support transferring data values from binding source objects
to binding targets, each object in your collection that supports
bindable properties must implement an appropriate property changed
notification mechanism such as the INotifyPropertyChanged interface.

吻泪 2024-12-19 17:44:34

以下是 StackOverflow 上实现 ObservableCollection 的一个示例,该示例在修改包含的元素时也会引发事件:

ObservableCollection 还监视集合中元素的更改

请参阅 里德·科普西在此线程中回答链接到一个项目,该项目实现了一个监听其子元素的 ObservableCollection。

Here is one example from here on StackOverflow of implementing an ObservableCollection that also raises events when contained elements are modified:

ObservableCollection that also monitors changes on the elements in collection

See Reed Copsey's answer in this thread for a link to a project that has implmented an ObservableCollection that listens to its child elements.

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