Through behavior to achieve
Separation of interface and logic
Window closing behavior: It contains a Boolean close property that binds the corresponding shutdown behavior to the property, which allows the window to be closed to allow the separation of the VM from view
Public classWindowclosebehaviour:behavior<window> { Public Static ReadOnlyDependencyProperty Closeproperty =Dependencyproperty.register ("Close", typeof(BOOL), typeof(windowclosebehaviour),NewFrameworkpropertymetadata (false, onclosechanged)); Public BOOLClose {Get{return(BOOL) GetValue (Closeproperty); } Set{SetValue (closeproperty, value);} } Private Static voidonclosechanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {varwindow =((windowclosebehaviour) d). Associatedobject; varNewValue = ((BOOL) e.newvalue); if(newvalue) {window. Close (); } }
View:
Xmlns:event= "Clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity"
And behaviors is the declared Windowclosebehaviour namespace.
< event:interaction. Behaviors> <Close= "{Binding Iswinclose} " /> </ event:interaction. Behaviors>
ViewModel:
Private BOOL_iswinclose; /// <summary> ///indicates whether the window is closed/// </summary> Public BOOLIswinclose {Get{return_iswinclose;} Set{_iswinclose=value; OnPropertyChanged ("Iswinclose"); } }
You only need to specify Iswinclose = True in the business logic to close the window;
Of course there are many ways to implement it, and it is possible to implement it with additional attributes; The behavior here is to specify the type to avoid the type judgment of the attached property.
Implementation of the close window in MVVM mode