Implementing “close window” command with MVVM

后端 未结 12 1707
既然无缘
既然无缘 2020-12-05 09:31

So my first attempt did everything out of the code behind, and now I\'m trying to refactor my code to use the MVVM pattern, following the guidance of the MVVM in the box inf

12条回答
  •  感动是毒
    2020-12-05 10:07

    My solution to close a window from view model while clicking a button is as follows:

    In view model

    public RelayCommand CloseWindow;
    Constructor()
    {
        CloseWindow = new RelayCommand(CloseWin);
    }
    
    public void CloseWin(object obj)
    {
        Window win = obj as Window;
        win.Close();
    }
    

    In View, set as follows

提交回复
热议问题