可观察集合集合已更改

发布于 2024-12-18 15:15:39 字数 488 浏览 1 评论 0原文

可观察集合的速度非常快。我一直在 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 技术交流群。

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

发布评论

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

评论(4

肤浅与狂妄 2024-12-25 15:15:39

我明白你想要做什么,但如果我正确,Observable Collection 仅在其集合中的项目发生更改时引发 INotifyCollectionChanged 事件。这将触发 UI 中的更改。

它不关心其集合中的某个对象的属性是否发生变化。您需要在这些对象的属性上实现 INotifyPropertyChanged 接口才能触发 UI 更改。

我读过此处,这给了我一些有用的见解。尽管它是针对 WPF 的,但它的大部分内容仍然适用,因为 Silverlight 本质上是 WPF 的子集。

以及这篇 MSDN 文章,其中指出:

 In particular, if you are using OneWay or TwoWay (for example, 
 you want your UI to update when the source properties change dynamically), 
 you must implement a suitable property changed notification mechanism 
 such as the INotifyPropertyChanged interface.

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:

 In particular, if you are using OneWay or TwoWay (for example, 
 you want your UI to update when the source properties change dynamically), 
 you must implement a suitable property changed notification mechanism 
 such as the INotifyPropertyChanged interface.
小瓶盖 2024-12-25 15:15:39

要获取 Person 类的更新,您应该实现 INotifyPropertyChanged 为他们服务。

To get updates for Person class you should implement INotifyPropertyChanged for them.

王权女流氓 2024-12-25 15:15:39

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.

很酷又爱笑 2024-12-25 15:15:39

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's CollectionChanged event. Don't forget to unhook the events when children are removed from the collection.

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