如何反映对其他窗口的 ViewModel 所做的更改?
我的应用程序包含以下模块:
SystemModel --> SystemViewModel --> SystemWindow
SettingsModel --> SettingsViewModel --> SettingsWindow
我的SystemViewModel 包含SettingsViewModel 和其他内容。 在我的SystemViewModel中,我创建了一些实例,其构造函数接收SettingsViewModel。
如果我想更新设置,我打开新的SettingsWindow作为ShowDialog,用SettingsViewModel作为他DataContext。如果用户单击“确定”,我会更新设置,否则我不会更新。
我的问题是我不知道如何更新我在 SystemViewModel 中创建的 Instance 中的 Settings (Instance > 在他的 ctor 中接收了 SettingsViewModel)。
有什么想法吗?
My app contains the following moduls:
SystemModel --> SystemViewModel --> SystemWindow
SettingsModel --> SettingsViewModel --> SettingsWindow
My SystemViewModel contain SettingsViewModel and other things.
In my SystemViewModel I create some Instance that its ctor receives SettingsViewModel.
If i want to update the settings I opened new SettingsWindow as ShowDialog with SettingsViewModel as him DataContext. If the user click "ok" I update the settings else I don't update.
My problem is that I dont know how to update the Settings in the Instance that I created in the SystemViewModel (Instance that received SettingsViewModel in his ctor).
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否可以仅创建一个设置视图模型实例,也许驻留在一个公共视图模型中,该视图模型充当视图模型的根并提供将模型绑定在一起的粘合剂?
像视图模型控制器之类的东西,即使这听起来有点奇怪。该根视图模型可以对来自视图模型的事件做出反应,然后可以执行更新其他设置所需的所有操作。
另一种方法是基于消息传递的方法,例如 MVVM Light Toolit 实现的方法。我曾经使用过一次,在项目变得相当大之后,这种方法在维护方面有点复杂。
更新:
您可以找到有关 MVVM Light Toolkit 的信息在这里。
Can you create only one instance of settings view model, maybe residing in a common view model that serves kind of a root for the view model and providing the glue that binds the models together?
Something like a view model controller, even if this sound a bit strange. This root view model could react to events from the view models and then can do everything that is required to update the other settings.
Another approach is a messaging based approach like the one that is implemented by MVVM Light Toolit. I have used this once and after the project got rather big this approach was kind of complicated regarding maintenance.
Update:
You can find information about MVVM Light Toolkit here.