MVVM命令绑定 [英] MVVM Command Binding

查看:157
本文介绍了MVVM命令绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习MVVM模式。 。我有在学习,我要声明,创建和绑定的命令对象存在的主要问题。

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的例子:


  1. 我有一个就像一个开关板或主菜单主要形式。 SELCT按钮1视图1显示,选择按钮2,显示视图。大。现在我想回去的主要形式,所以我需要在视图1(和查看2)被称为主菜单按钮。我应该在哪里定义命令和命令处理程序,这样我可以绑定到ShowMainMenu命令?我可以在View2ViewModel创建他们,但当时我没有访问,以显示主视图?或者,我可以在模型的MainView创建thim但我怎么绑定到他们在子视图模型(我使用的RelayCommand obejct按照该MVVM建议,他们不冒泡到父。)

  1. 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.)

我有两个用户控制在一个主窗口视图中可见我们姑且称之为的MainView,UC1和UC2。每一种都有视图模型MainViewModel,UC1ViewModel,UC2View模型。我有一个UC1按钮叫的AddItem。它应该在UC2列表中添加项目。
什么是设立AddItemCommand,并绑定到它确认当期的方式。如果命令是MainViewModel,Uc1ViewModel或UC2ViewModel?我如何768,16绑定到它。

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.

感谢您的帮助。

推荐答案

1)你可以从一个基站的ViewModel继承View1Model和View2Model并定义ShowMainMenu那里。

1) You can inherit View1Model and View2Model from one base ViewModel and define ShowMainMenu there.

或(它是我的方法)

创建ContentPresenter RootView它会显示所有的观点。创建具有财产ViewContent RootVeiwModel。 ContetnPresenter的内容绑定到propertty ViewContent RootViewModel的财产。您可以使用对象类型ViewContent,但我劝你定义了由MainVView1Model,View1Model和View2Model支持的接口。 ViewContent的更改必须提高ProprtyChangedEvent。
定义ShowMainViewCommand在RootViewModel这将只是改变ViewContent为MainViewModel(它会显示为的MainView)。然后在视图1和视图2巴顿指挥的属性,方法绑定到该命令,exmple:

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:

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RootView}}, 
                         Path=DataContext.ShowMainViwe}

有一些代码来解释我想说的:

There is some code to explain what I'm trying to say:

RootView.xaml

RootView.xaml

...
<ContentPresenter Content={Binding ViewContent} />
...



RootViewModel.ca

RootViewModel.ca

class RootViewModel : INotifyPropertyCahnged
{
    ...
    private object _ViewContent;
    public object ViewContent
    {
        get {return _ViewContent;}
        set
        {
            _ViewContent = value;
            if (PropertyChanged != null)
            {
                PropertyChanged ("ViewContent");
            }

        }
    }

    private RelayCommand _ShowMainView;
    public ICommand ShowMainView
    {
        get 
        {
            if (_ShowMainView == null)
            {
                _ShowMainView = new RelayCommand(x => ViewContent = new MainViewModel());
            }
            return _ShowMainView;
        }
    }
    ...
}

2)参考MainViewModel加入UC1ViewModel和UC2ViewModel - 这就是影响其他控件的方式。 。MainViwModel必须包含包含第二个用户控制的UC1ViewModel和UC2ViewModel项目必须包含在的ObservableCollection性能

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:

class UC1ViewModel : INotifyPropertyChanged
{
    ...
    private MainViewModel _Parent;
    public UC1ViewModel(MainViewModel parent)
    {
        _Panert = parent;
    }

    private RelayCommand _AddItemToUC2;
    public ICommand AddItemToUC2
    {
        get
        {
            if (_AddItemToUC2 = null)
            {
                // UC2Content is UC2ViewModel
                // Items is ObservableCollection
               _AddItemToUC2 = new RelayCommand(x => _Parent.UC2Content.Items.Add(...));
            }
            return AddItemToUC2;
        }
    }
    ...
}

这篇关于MVVM命令绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆