MVVM 命令绑定
我正在尝试学习 MVVM 模式。我遇到的主要问题是学习应该在哪里声明、创建和绑定命令对象。
2 个示例:
我有一个主窗体,其作用类似于开关板或主菜单。选择按钮 1 并显示视图 1,选择按钮 2 并显示视图 2。伟大的。现在我想返回主窗体,因此我需要在视图 1(和视图 2)上有一个名为“主菜单”的按钮。我应该在哪里定义命令和命令处理程序,以便可以绑定到“ShowMainMenu”命令?我可以在 View2ViewModel 中创建它们,但是我无权显示主视图?或者,我可以在 MainView 模型中创建它,然后如何在子视图模型中绑定到它们(我根据 mvvm 建议使用 RelayCommand 对象,它们不会冒泡到父级。)
我在单个主窗口视图上有两个可见的用户控件,我们将它们称为 MainView、UC1 和 UC2。其中每个都有 ViewModel MainViewModel、UC1ViewModel、UC2View Model。我在 UC1 上有一个名为“AddItem”的按钮。它应该在 UC2 上的列表中添加一个项目。 设置“AddItemCommand”并绑定到它的正确方法是什么?命令应该位于 MainViewModel、Uc1ViewModel 还是 UC2ViewModel 中?我应该如何绑定到它。
感谢您的帮助。
I'm trying to learn the MVVM pattern. The main problem I'm having is learning where I should be declaring, creating and binding command objects.
2 examples:
I have a main form that acts like a switch board or main menu. Selct button 1 and View 1 is displayed, Select button 2 and view 2 is displayed. Great. Now I want to go back to the main form so I need a button on View 1 (and view 2) called "Main Menu". Where should I define the command and command handlers so that I can bind to the "ShowMainMenu" command? I could create them in the View2ViewModel but then I don't have access to show the Main View? Or, I could create thim in the MainView model but then How do I bind to them in the child view model (I'm using the RelayCommand obejct as per the mvvm recommendation and they don't bubble up to the parent.)
I have two user controls visible on a single Main Window view let's call them MainView, UC1 and UC2. each of these has ViewModel MainViewModel, UC1ViewModel, UC2View Model. I have a button on UC1 called "AddItem". It should add an item in a list on UC2.
What is the currect way to set up an "AddItemCommand" and bind to it. Should the Command be in MainViewModel, Uc1ViewModel or UC2ViewModel? And How shoud I bind to it.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 您可以从一个基本 ViewModel 继承 View1Model 和 View2Model 并在那里定义 ShowMainMenu。
或者(这是我的方法)
使用 ContentPresenter 创建 RootView ,它将显示您的所有视图。使用属性 ViewContent 创建 RootVeiwModel。将 ContetnPresenter 的 Content 属性绑定到 RootViewModel 的 ViewContent 属性。您可以使用
object
作为ViewContent的类型,但我建议您定义MainVView1Model、View1Model和View2Model支持的接口。 ViewContent 的更改必须引发 ProprtyChangedEvent。在 RootViewModel 中定义 ShowMainViewCommand ,这只会将 ViewContent 更改为 MainViewModel (并且它将显示为 MainView)。然后将 View1 和 View2 中 Button 的 Command 属性绑定到该命令,例如:
有一些代码可以解释我想说的内容:
RootView.xaml
RootViewModel.ca
2) 将 MainViewModel 的引用添加到 UC1ViewModel 和 UC2ViewModel -这就是影响其他控件的方式。 MainViwModel 必须包含包含 UC1ViewModel 和 UC2ViewModel 的属性 第二个用户控件的项必须包含在 ObservableCollection 中。
我只是通过代码向您展示它是如何工作的:
1) You can inherit View1Model and View2Model from one base ViewModel and define ShowMainMenu there.
or (it's my approach)
Create RootView with ContentPresenter which will show all of your views. Create RootVeiwModel with property ViewContent. Bind Content propertty of ContetnPresenter to ViewContent property of RootViewModel. You can use
object
as type of ViewContent, but I advise you to define the interface that is supported by MainVView1Model, View1Model and View2Model. Changing of ViewContent must raise ProprtyChangedEvent.Define ShowMainViewCommand in RootViewModel which will just change ViewContent to MainViewModel (and it will show as MainView). Then bind Command property of Button in View1 and View2 to that command, for exmple that way:
There is some code to explain what I'm trying to say:
RootView.xaml
RootViewModel.ca
2) Add reference to MainViewModel to UC1ViewModel and UC2ViewModel - thats the way to influence other controls. MainViwModel must contain properties that contains UC1ViewModel and UC2ViewModel Items of second user control must be contained in ObservableCollection.
I just show you how it works by the code:
MainModel 可以为每个 UCxViewModel 提供一个属性,或者更简单的是,为 ViewModel 列表提供一个属性。
“Show”命令将创建相应的 UVxViewModel,订阅 UVxViewModel 发布的“OnClose”事件,并将其添加到列表中。
MainView 有一个绑定到此列表的控件(例如选项卡控件)和定义要用于每个 UCxViewModel 的视图的 DataTemplates。
当 UVxViewModel 触发其 OnClose 事件时,MainModel 将其从列表中删除,从而导致相应的视图“关闭”。
对于“添加项目”部分,ViewModels 应共享相同的项目列表(模型)。然后 UC2ViewModel 可以添加一个项目,并且 UC1View 将得到更新(假设列表实现了 INotifyCollectionChanged)。
我发现这个解释对于理解MVVM非常有帮助。
The MainModel could have a property for each UCxViewModel or, easier, a list of ViewModels.
The "Show" command would create a corresponding UVxViewModel, subscribe to a "OnClose" event published by the UVxViewModel, and add it to the list.
The MainView has a control (Tab Control for example) bound to this list and DataTemplates defining the Views to be used for each UCxViewModel.
When the UVxViewModel fires its OnClose event, the MainModel removes it from the list, causing the corresponding view to "close".
For the "Add Item" part, the ViewModels should share the same list of items (the Model). The UC2ViewModel can then add an item and the UC1View would get updated (provided the list implements INotifyCollectionChanged).
I found this explanation very helpful in understanding MVVM.