Moving MVVM Commands

I am trying to learn the MVVM pattern. The main problem I am facing is finding out where I should declare, create and link command objects.

2 examples:

  • I have a basic form that acts like a switch panel or main menu. Selct 1 and View 1 button will appear, select button 2 and view 2 will be displayed. Excellent. Now I want to return to the main form, so I need a button in View 1 (and view 2) called "Main Menu". Where should I define command and command handlers so that I can attach to the ShowMainMenu command? I could create them in View2ViewModel, but then I do not have access to display the Main View? Or I could create a tim in the MainView model, but then how do I bind a child view to them in the model (I use Obejct RelayCommand as recommended by mvvm, and they don’t go to the parent.)

  • I have two user controls visible in one view of the main window, which call them MainView, UC1 and UC2. each of them has a ViewModel MainViewModel, UC1ViewModel, UC2View Model. I have a button on UC1 called "AddItem". He must add the item to the list on UC2. What is the correct way to configure "AddItemCommand" and bind to it. Should the command be in MainViewModel, Uc1ViewModel or UC2ViewModel? And how I get attached to him.

Thank you for your help.

+5
source share
2 answers

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

or (this is my approach)

RootView ContentPresenter, . RootVeiwModel ViewContent. contenttty ContetnPresenter ViewContent RootViewModel. object ViewContent, , MainVView1Model, View1Model View2Model. ViewContent ProprtyChangedEvent. ShowMainViewCommand RootViewModel, ViewContent MainViewModel ( MainView). Command Button View1 View2 , :

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

, , :

RootView.xaml

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

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.

, :

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;
        }
    }
    ...
}
+3

MainModel UCxViewModel , , ViewModels. "" UVxViewModel, "OnClose", UVxViewModel, . MainView (, Tab Control), , DataTemplates, , UCxViewModel. UVxViewModel OnClose, MainModel , "".

" " ViewModels (). UC2ViewModel , UC1View ( INotifyCollectionChanged).

MVVM.

+2

All Articles