Implementation of the close window in MVVM mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.