ObservableCollections 和 C# 中属性的更改
我正在使用我定义的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
Job< 上实现 INotifyPropertyChanged 接口< /代码> 类。然后,您应该可以在
ObservableCollection
上使用PropertyChanged
。Implement the INotifyPropertyChanged interface on your
Job
class. This should then allow you to use thePropertyChanged
on yourObservableCollection<Job>
.以下是 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.