c# - MVVM Command Binding -
I am trying to learn the MVVM pattern. The main problem is that I am learning that where should I make, declare and force the command object.
2 example:
-
I have a main form that works like a switch board or main menu appears in the salts button 1 and see 1, Select button 2 and see 2 Is displayed. great. Now I want to return to the main form so that I need a button on scene 1 (and see 2) called "Main menu" Where should I define command and command handles so that I can tie in the "ShowMainMenu" command? I can make them in View2ViewModel but I do not have access to show main view? Or, I can make a theme in the main visual model, but then how do I wrap them in the hair visual model? (I am using Relay Commands object as per MVVM Recommendation and they do not bubble over parent.)
-
I have a visible view on a single main window view There are two user controls, call them on Main View, UC1 and UC2. Each of them has ViewModel MainViewModel, UC1ViewModel, UC2View models. I have a button on the UC1 called "AdiTom", it should add an item to a list on UC2. What is the correct way to set up "AddItemCommand" and to bind it, should the command be in MainViewModel, Uc1ViewModel or UC2ViewModel?
Or (this is my viewpoint ) Create
RootView with ContentPresenter which will show all your thoughts Create RootWee models with property view content. Content property of the RouteWidmodel to see content property of contentpager. You can use the object
as the type of ViewContent, but I recommend you to define the interface which is supported by the main VView1Model, View1Model and View2Model. Changing ViewContent should change PropRtyChangedEvent Define ShowMainViewCommand in RootViewModel which will now change ViewVontroll ViewViewModel (and it will show as main view). Next, in that order, force the command property of the scene 1 and 2 of the button, for the exmple way:
{binding relative Soros = {relative asses FindAncestor, AncestorType = {x} : Type RootView}}, path = data content .ShowMainViwe}
What I'm trying to say is something to clear:
RootView. Xaml
... & Lt; Content Presentation Content = {Binding Visual Communication} / & gt; ...
RootViewModel.ca
class RootViewModel: INotifyPropertyCahnged {... private object_ViewContent; Set up the public object viewer {get {VIEW_ViewContent;} {_ViewContent = value; If (property changed! = Null) {property changed ("visual communication"); }}} Private Relay Commands _ Showmanview; Public ICommand ShowMainView {Get (_ShowMainView == empty) {_ShowMainView = New Relay Command (x => ViewContent = New MainViewModel ());} Back _ShowMainView;}} ...}
2) Add the context of the main visual model to the UC1ViewModel and UC2ViewModel - which is the way to affect other controls. MainViwModel should include a property that contains items of UC1ViewModel and UC2ViewModel other user controls should be included. ObservableCollection
I just show you how this code works by:
class UC1ViewModel: INotifyPropertyChanged {... Private MainViewModel _Parent; Public UC1ViewModel (MainViewModel parent) {_Panert = parent; } Private Relay Commands _ EditToucci2; Public ICommand AddItemToUC2 {Receive (if (_AddItemToUC2 = null) {// UC2ontent is UC2ViewModel / Item Overview Pulling _AddItemToUC2 = New Relay Commands (x = & gt; _Parent.UC2Content.Items.Add (...))}} Return AddItemToUC2;}} ...}
Comments
Post a Comment