WPF: Under MVVM call ViewModel View mode

Two basic approaches:  message notification and parameter passing

First, the message notification

View property use in the IsEnable

The principle is this:

1, UI IsEnabled binding in the VM properties

2, the code behind the UI, the registration IsEnableChange event, in this event, the detected value to pass over to meet certain conditions to trigger Close () command

So, VM control their own property that can achieve the purpose of the closed V.

Second, the parameters are passed

Depending on the parameters passed. The transfer function is divided into objects and transmitting View.

Transfer Function

The method: requires three steps.

Override the constructor of the ViewModel

public ProduceCloud_ViewModel(Action close)
        {
            this.close = close;           
        }

View ViewModel in the new object, the object is passed to the Close method View ViewModel.

public ProduceDsm_View()
        {
            InitializeComponent();
            
            produceCloudViewModel = new ProduceCloud_ViewModel(this.Close);
            this.DataContext = produceCloudViewModel;
        }

 When executing the need to close an operation View, delegate the function to call.

 private void Act_OnClick(object obj)
        {

         //其他操作
            this.close.Invoke();

        }

View object transfer

Override the constructor of the ViewModel

class MyViewModel 
    { 
        ///  <Summary> 
        /// Add Window properties
         ///  </ Summary> 
        Private Window window { GET ; SET ;} 

        ///  <Summary> 
        /// constructor receiving window as a parameter
         ///  </ Summary> 
        public MyViewModel ( Object window) 
        {           
            the this .window = (the window) window;            
        } 

    }

New ViewModel instance when passing View object.

public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MyViewModel(this);
        }

When necessary, to call the window's Close method.

this.window.Close();

 

Guess you like

Origin www.cnblogs.com/jasonlai2016/p/12302265.html