wpf mvvm 使用命令在视图模型之间传递参数
这是我第一次尝试 MVVM。我的应用程序的核心松散地基于 Josh Smith 的 msdn 文章。而且我也在使用 mvvm light 框架。
我有一个主窗口,其中包含命令列表区域和工作区区域,其中将用户控件/视图显示为选项卡项,每个用户控件都有一个相应的视图模型。主窗口还有一个包含我的命令列表的视图模型,工作区视图模型有一个基础工作区视图模型。
我的默认视图有一个 MappingSets 的主数据网格,它可以包含一个选定的项目。这些命令启动新的选项卡项,其中包含基于所选项目处理 MappingSet 详细信息的视图。我有一个 View/ViewModel,根据所使用的命令,应该返回一个用于创建没有现有数据的新 MappingSet 的 tabitem,或者一个包含要编辑的所选项目的详细信息的 tabitem,或者一个包含所选项目的详细信息的 tabitem,如新映射集的基础。
设置场景后,我没有设法解决的是命令相关的方式来传递参数,例如所选 MappingSet 对象的标识符,以在上述三种状态之一中实例化我的视图模型?例如,mvvmlight Messenger 适合这项任务吗?
This is my first attempt at MVVM. My application's core is loosely based Josh Smith's msdn article. And I am also using the mvvm light framework.
I have a main window containing a command list area and a workspace area which shows usercontrols/views as tabitems, each usercontrol has a corresponding viewmodel. The mainWindow also has a viewmodel containing my command list, and the workspace viewmodels have a base workspace viewmodel.
My default view has a master datagrid, of MappingSets, that can have one selected item. The commands launch new tabitems with views that handle MappingSet detail based on that selected item. I have a View/ViewModel that, depending on the command used should return either a tabitem for creating a new MappingSet with no existing data, or a tabitem containing the detail of the selected item for editing, or a tabitem containing detail the selected item as the base for a new MappingSet.
Having Set the scene, what I have not managed to work out is command dependent way to pass parameters, such as the identifier of the selected MappingSet object, to instantiate my viewmodel in one of the three states mentioned above? For instance would the mvvmlight messenger be appropriate for this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于消息传递/事件聚合器来说,这是一个完美的场景。但是,您的消息链可能有点复杂。据我了解,您的主窗口包含命令列表(如菜单或功能区)。以下是我对事件链的看法。
This is a perfect scenario for the messenger/eventaggregator. However, your message chain might be a bit convoluted. From what I'm understanding, your Main window holds a list of commands (like a menu or a ribbon). Here is how I see the chain of events.
如果只有三个选项,您可以将它们绑定到三个不同的命令,并在命令中传递您的自定义变量。
然后在视图模型的构造函数中允许该参数,并根据该参数执行您需要的其他操作。
If there are only three options, you could have them binding to three different commands and within the commands do the passing of your self-defined variable.
Then allow for that parameter on the constructor of your viewmodel and do whatever else you need based on that.