如何使用 MVVM 设计从 WPF 中的视图模型更新视图
我在第一个项目中使用带有 MVVM 设计的 WFP,在处理来自客户端的更新实体的命令后,我遇到更新视图的问题。此时,视图可以与视图模型对话,但视图模型无法与视图对话。 任何人都知道如何使其有效? 谢谢, 吉当
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使 ViewModel 实现 INotifyPropertyChanged 并在 ViewModel 属性更改时触发 PropertyChanged 事件。假设 UI 绑定到 ViewModel 属性,这应该可以工作。
You should make the ViewModel implement INotifyPropertyChanged and fire the PropertyChanged event when the ViewModel properties change. Assuming that the UI is bound to the ViewModel properties this should work.
在典型的 MVVM 应用程序中,您使用绑定将视图连接到 ViewModel。当 ViewModel 引发由
INotifyPropertyChanged
接口定义的PropertyChanged
事件时,绑定会自动更新。因此,您需要在 ViewModel 中实现该接口,并在属性值更改时引发PropertyChanged
事件,视图将自动反映更改。In a typical MVVM application, you use bindings to connect the view to the ViewModel. Bindings are automatically updated when the ViewModel raises the
PropertyChanged
event, defined by theINotifyPropertyChanged
interface. So you need to implement that interface in the ViewModel and raise thePropertyChanged
event when the value of a property is changed, and the view will reflect the change automatically.如果您是 MVVM 模式的新手,我推荐以下来自 MSDN 的优秀资源,其中涵盖了该模式以及如何在 WPF 和 Silverlight 应用程序中实现它:
根据您所说,听起来您可能想回顾一下数据绑定,以及如何进行利用 INotifyPropertyChanged、INotifyCollectionChanged 和 ICollectionView 接口,支持视图和视图模型之间的双向通信。
Silverlight 和 WPF 数据绑定支持多种数据绑定模式。通过单向数据绑定,UI 控件可以绑定到视图模型,以便它们在呈现显示时反映基础数据的值。当用户在 UI 中修改底层数据时,双向数据绑定也会自动更新底层数据。为了确保当视图模型中的数据发生更改时 UI 保持最新,它应该实现适当的更改通知接口。
If you are new to the MVVM Pattern, I recommend the following as excellent resources from MSDN that cover both the pattern and how to implement it in WPF and Silverlight applications:
Based on what you have said, it sounds like you might want to review data binding, and how you can leverage the INotifyPropertyChanged, INotifyCollectionChanged, and ICollectionView interfaces to enable two-way communication between your views and view models.
Silverlight and WPF data binding supports multiple data binding modes. With one-way data binding, UI controls can be bound to a view model so that they reflect the value of the underlying data when the display is rendered. Two-way data binding will also automatically update the underlying data when the user modifies it in the UI. To ensure that the UI is kept up to date when the data changes in the view model, it should implement the appropriate change notification interface.
除了其他答案之外,我还建议您的 ViewModel 扩展 DependencyObject。
有些人认为 DependencyObject 很重(如果您创建了数千个实例,那么它们也可能很重)并且对于新用户来说有点复杂(肯定在某些情况下确实如此)。然而,DependencyObjects 还有其他优点,例如自动支持属性更改通知和绑定评估速度。
这是我的 DP 片段(在 C:\Users[您的名字]\Documents\Visual Studio [2010, 2008]\Code Snippets\Visual C#\My Code Snippets 中另存为 DependencyProperty.snippet):
In addition to the other answers, I would also suggest having your ViewModels extend DependencyObject.
Some people believe DependencyObjects are heavy weight (and they can be, if you create thousands of instances of them) and a bit complex for new users (most definitely there are situations where this is true). There are, however, other advantages to DependencyObjects, such as automatic support for property change notification and speed of binding evaluation.
Here's my DP snippet (save as DependencyProperty.snippet in C:\Users[YOUR NAME HERE]\Documents\Visual Studio [2010, 2008]\Code Snippets\Visual C#\My Code Snippets):