WPF:MVVM Mode ViewModel Close view

Source: Internet
Author: User

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

Related Article

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.