当模型更改但属性未更改时如何通知 MVVM 中的视图
我已将列表视图中的项目绑定到 ViewModel 中的 observableCollection(我有两个列表视图,每个视图都有自己的 observableCollection)。我还将几个状态栏属性绑定到 ViewModel 中的属性。
在我的视图模型中,我有一些特殊类型的复制/粘贴函数来复制“内存”缓冲区。实际上,我的视图模型(以及下面的模型)中的属性只不过是指向这些内存缓冲区的“指针”。复制时,我知道属性已更改,但值本身不会通过设置器更改(因此不会调用 INotifyPropertyChanged)。
如何刷新列表视图? 状态栏并不是那么困难,因为我可以调用视图模型中的方法来重新计算值。然而对于列表视图的数据来说这是相当不切实际的。
在非 MVVM 中,我只会用数据再次填充它们,但对于 MVVM/绑定来说这是不可能的(我认为)。
I have bound in my list views the items to an observableCollection in my ViewModel (I have two list views, each with their own observableCollection). I also bounded several status bar properties to properties in the ViewModel.
In my view model, I have some special kind of copy/paste functions that copy 'memory' buffers'. Actually the properties in my view model (and model below) are nothing more than 'pointers' into those memory buffers. When copying I know that properties are changed, but the values themselves are not changed with a setter (so the INotifyPropertyChanged is not called).
How can I refresh the list views?
The status bar is not that difficult because I can call a method in the view model to recalculate the values. However for the list view's data that is quite impractical.
In non MVVM I would just refill them again with data but that is not possible (I think) with MVVM/binding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,在您的视图模型中,您有一个 ObservableCollection 项目(例如 ObservableCollection)。您需要做的是确保MyItem(即集合中的每个项目)派生自INotifyPropertyChanged。然后,当您更改集合中的项目时,触发 PropertyChanged 事件。 WPF 列表视图将拾取它。
其他评论者描述的 RaisePropertyChanged 是通常添加到 VM 类(通常添加到所有 VM 类的公共基类)的实用程序方法。它引发 PropertyChanged 事件:
正如其他评论者所描述的,没有什么规定您只能从 setter 内部引发 PropertyChanged 事件。在 Copy 方法中,只需使用适当的属性名称调用 RaisePropertyChanged(对于每个已更改的属性,您可以多次调用它)。
编辑:对于状态行,如果您想遵循 MVVM 设计模式,则不应从 VM 调用视图方法(这是 MVVM 中的一个关键概念:ViewModel 与视图无关)。您应该简单地为控制状态行的所有属性调用 RaisePropertyChanged 方法。
So, in your view model you have an ObservableCollection of items (say ObservableCollection). What you need to do is to make sure that MyItem (that is, each item in the collection) derives from INotifyPropertyChanged. Then, when you change the items within the collection, fire the PropertyChanged event. The WPF List View will pick it up.
The RaisePropertyChanged described by other commentors is a utility method typically added to VM classes (often, to a common base class of all VM classes). It raises the PropertyChanged event:
As described by other commentors, there is nothing that dictate you raise a PropertyChanged event only from within the setter. Within your Copy method, just call the RaisePropertyChanged with the appropriate property names (you can call it multiple times, for each property that has changed).
EDIT: For the status line, if you want to follow the MVVM design pattern, you shouldn't call methods from the VM to the View (that's a key concept in MVVM: The ViewModel is View agnostic). You should simply call the RaisePropertyChanged method for all the properties that control the status lines.
根据评论的解决方案:
复制这些内存缓冲区时发出适当的通知:使用
Solution according to the comments:
Raise a proper notification when copying these memory buffers: Use