WPF MVVM: How to close a window

后端 未结 21 1735
后悔当初
后悔当初 2020-12-04 08:19

I have a Button that closes my window when it\'s clicked:


21条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 08:36

    We have the name property in the .xaml definition:

    x:Name="WindowsForm"
    

    Then we have the button:

    Then in the ViewModel:

    public DelegateCommand   CloseCommand { get; private set; }
    
    Constructor for that view model:
    this.CloseCommand = new DelegateCommand(this.CloseAction);
    
    
    

    Then at last, the action method:

    private void CloseAction (object obj)
    {
      Window Win = obj as Window;
      Win.Close();
    
    }
    

    I used this code to close a pop-up window from an application..

    提交回复
    热议问题