可观察集合集合已更改
可观察集合的速度非常快。我一直在 Silverlight 中玩弄这些,做一些绑定的事情,你有什么。似乎从集合中删除或添加到集合时会触发 CollectionChanged 事件。当我更改集合中某个类的属性时,我希望有一些东西能够触发。集合属性本身已经具有 RaisePropertyChanged。我需要对类型类本身做一些特殊的事情吗?因此,如果我有这样的情况:
ObservabelCollection<Person> personcollection... and if I change a property like:
Person p = personcollection.where(e => e.FirstName == "Joey").FirstOrDefault();
if (p != null) { p.FirstName = "Joe"; }
我希望 UI 中发生一些事情,但没有任何变化。
任何帮助将不胜感激。
大卫
Real quick on observable collections. I've been playing around with these in Silverlight doing some binding stuff, what have ya. Seems like the CollectionChanged event fires when removing from or adding to the collection. I'd like for something to fire when I change a property on one of the classes inside the collection. The collection property itself already has the RaisePropertyChanged. Do I need to do something special to the type class itself? So if I have this:
ObservabelCollection<Person> personcollection... and if I change a property like:
Person p = personcollection.where(e => e.FirstName == "Joey").FirstOrDefault();
if (p != null) { p.FirstName = "Joe"; }
I would expect something to happen in the UI but nothing changes.
Any help would be greatly appreciated.
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我明白你想要做什么,但如果我正确,Observable Collection 仅在其集合中的项目发生更改时引发
INotifyCollectionChanged
事件。这将触发 UI 中的更改。它不关心其集合中的某个对象的属性是否发生变化。您需要在这些对象的属性上实现 INotifyPropertyChanged 接口才能触发 UI 更改。
我读过此处,这给了我一些有用的见解。尽管它是针对 WPF 的,但它的大部分内容仍然适用,因为 Silverlight 本质上是 WPF 的子集。
以及这篇 MSDN 文章,其中指出:
I see what you're trying to do but if im correct Observable Collection only raises the
INotifyCollectionChanged
event when items in it's collection changes. This will trigger changes in the UI.It doesn't care if a property on one of it's objects in it's collection changes. You would need to implement the
INotifyPropertyChanged
Interface on the properties on these objects to trigger a change to the UI.I had a read of here which gave me some useful insight. Though it is aimed for WPF most of it still applies as Silverlight is essentially a subset of WPF.
As well as this MSDN article which states:
要获取
Person
类的更新,您应该实现 INotifyPropertyChanged 为他们服务。To get updates for
Person
class you should implement INotifyPropertyChanged for them.INotifyCollectionChanged 应该在集合更改时通知某人。
INotifyPropertyChanged 应该在对象的属性发生更改时通知某人。
类的对象必须实现接口并且必须正确触发事件。因此,如果您通过 Binding 对象绑定它,UI 可以自行更新。
ObservableCollection 正确实现了 INotifyCollectionChanged,但它与它包含的对象无关。因为如果对象的属性发生更改,这不是 CollectionChanged 事件。这是集合中具体对象的 PropertyChanged 事件。因此,您必须在 Person 类中实现 INotifyPropertyChanged 接口。
INotifyCollectionChanged supposed to notify somebody when the collection changed.
INotifyPropertyChanged supposed to notify somebody when a property of an object changed.
An object of a class must implement the interface and must fire the events correctly. So if you bind it via the Binding object, the UI can update itself.
ObservableCollection implements INotifyCollectionChanged correctly, but it has nothing to do with the objects it contains. Because if an object's property changed that is not a CollectionChanged event. That's a PropertyChanged event of the concrete object within the collection. So you have to implement the INotifyPropertyChanged interface in your Person class.
ObservableCollection
不会报告对集合中对象的属性所做的更改。要获得该行为,您必须在每个子对象插入到集合中时为每个子对象挂钩
INotifyPropertyChanged.PropertyChanged
事件。您可以在集合的CollectionChanged
事件中执行此操作。当子项从集合中删除时,不要忘记取消事件。The
ObservableCollection
does not report changes made to properties of the objects in the collection.To get that behavior, you will have to hook the
INotifyPropertyChanged.PropertyChanged
event for each child object as they are inserted into your collection. You can do this in the collection'sCollectionChanged
event. Don't forget to unhook the events when children are removed from the collection.