如何通知可观察集合中项目的属性已更改?
好的,我在 XAML 文件中有一个 ItemsControl,它绑定到 ObservableCollection。 ObservableCollection 位于视图模型类(我们将此类称为 ViewModelA)中,而 ObservableCollection 中的每个项目都是另一个视图模型类(我们将其称为 ViewModelB 类)的实例。
ViewModelA 上有一个属性,当更改该属性时,将间接更改在 ViewModelB 类的许多实例中找到的属性值。换句话说,它不会直接进入 ViewModelB 并设置其属性,从而导致 INotifyPropertyChange 调用,而是进入模型,在我的模型中设置某些属性,并且我的模型中的更改会影响 ViewModelB 应显示的内容的观点。
如何通知视图 ViewModelB 中的某些内容已更改?
Okay, I have an ItemsControl in a XAML file that binds to an ObservableCollection. The ObservableCollection is found on a view-model class (let's call this class ViewModelA), and each item in the ObservableCollection is an instance of another view-model class (let's call the class ViewModelB).
There is a property on ViewModelA, that, when changed, will indirectly change the values of properties found in many instances of the ViewModelB class. In other words, it doesn't go straight to ViewModelB and set its properties, thus causing an INotifyPropertyChange call, but rather goes down to the model, sets some property in my model, and that change in my model affects what ViewModelB should be showing the view.
How can I notify the view that something in ViewModelB has changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够告诉视图集合已更改,然后触发它重新绑定到整个集合(这将更新视图)。
如果您的模型实现了 INotifyPropertyChanged,另一个选择是让您的 ViewModelB 类侦听其包装模型的更改,并根据需要引发属性更改事件。
You should be able to tell the View that the collection has changed, and in turn, trigger it to rebind to the entire collection (which would update the View).
If your Model implements
INotifyPropertyChanged
, the other option would be to have yourViewModelB
class listen for changes on it's wrapped Model, and raise property changed events as needed.理想情况下,您可以像 Reed Copsey 指出的那样...在模型上实现 INotifyPropertyChanged 并让 ViewModelB 侦听这些事件。然后,无论更新发生在哪里,ViewModelB 都会获取更改。
但是,在某些情况下,模型不会(或不能)实现 INotifyPropertyChanged。在这种情况下,您可能需要考虑使用事件聚合器模式在 ViewModelA 和 ViewModelB 实例之间传递消息。
在这种情况下,您可以从 ViewModelA 发布“模型已更改”消息。 ViewModelB 实例将订阅此消息,并且当 A 发布该消息时,每个实例都会收到通知。然后 ViewModelB 可以引发适当的 PropertyChanged 事件来告诉 UI 发生了什么变化。
您可以在许多框架中找到有关事件聚合器的更多信息,包括
Ideally, you'd be able to do like Reed Copsey indicated... Implement INotifyPropertyChanged on the model and have ViewModelB listen for those events. Then ViewModelB will pick up the changes no matter where the update happens.
However, In some cases the model doesn't (or can't) implement INotifyPropertyChanged. In this case, you may want to consider using an Event Aggregator pattern to pass a message between ViewModelA and the ViewModelB instances.
In this case, you could publish a "model changed" message from ViewModelA. The ViewModelB instances would subscribe to this message and each of them would get notified when A published the message. Then ViewModelB could raise the approriate PropertyChanged events to tell the UI what's changed.
You can find more info on Event Aggregator in many of the frameworks, including
为了解决这个问题,我创建了一个名为 VeryObservableCollection 的类。对于您添加的每个对象,它会将对象的 NotifyPropertyChanged 事件挂接到触发 CollectionChanged 事件的处理程序。对于删除的每个对象,它都会删除处理程序。非常简单,并且会给你你想要的。部分代码:
To solve this I created a class called VeryObservableCollection. For each object you add, it hooks the object's NotifyPropertyChanged event to a handler that triggers a CollectionChanged event. For each object removed, it removes the handler. Very simple and will give you exactly what you want. Partial code:
如果模型中的更改将更改 ViewModelB 的某些属性,并且这些属性已向 UI 发出更改通知(即 ViewModleB 正在实现 INotifyPropertyChanged),则更改将立即反映在 UI 中。
因此,如果您有另一个 viewmodelB 的 ObservableCollection,则无需为该 viewmodelB 的 propertychanged 连接事件。根据我的理解,无论谁更改了 viewmodelB 的属性(模型类或其他任何人)并且如果属性有更改通知,视图都会自动更新。
If change in Models will change in some properties of ViewModelB and those properties has change notification to UI (i.e. ViewModleB is implementing INotifyPropertyChanged), the change will immediately reflect in the UI.
So, if you have a ObservableCollection of another viewmodelB, you need not to hook up events for the propertychanged of that viewmodelB. According to my understanding, whoever changes viewmodelB's properties (model class or anyone else) and if the properties has change notification, the view will update automatically.