There are two basic methods.
Message notification and parameter passing.
I. Notification of the News
Using the Isenable attribute in view
The principle is this:
1. IsEnabled in UI bind properties in VM
2, the background code of the UI, register the Isenablechange event, in this event, the detected value passed to meet a certain condition, you can trigger the close () command
In this way, the VM controls its own property to achieve the purpose of shutting down v.
Second, the parameter pass.
Varies according to the parameters passed. is divided into transfer functions and pass view objects.
1 transfer function
This method: takes three steps.
1. Overriding the ViewModel constructor
Public Producecloud_viewmodel (Action close) { this.close = close; }
2. When you create a new ViewModel object, view passes the Close method of the View object to ViewModel.
Public Producedsm_view () { InitializeComponent (); New Producecloud_viewmodel (this. Close); this. DataContext = producecloudviewmodel; }
3 The function delegate is called when a view is required to close when an operation is completed.
Private void Act_onclick (object obj) { // Other operation this . Close. Invoke (); }
2 Passing the View object
This is easy.
1 overriding the ViewModel constructor
ClassMyviewmodel {///<summary>/// Add Window Property/// </summary> Private window window { get; set;} ///<summary>/// Constructor, receive window as parameter ////</summary> Public Myviewmodel (Object window) { This.window = (window) window; }}
3 When creating a new ViewModel instance, pass in the View object.
Public MainWindow () { InitializeComponent (); This New Myviewmodel (this); }
3 when needed, call the Close method of window.
this. window. Close ();
WPF:MVVM Mode ViewModel Close view