How to Bind to window's close button the X-button

后端 未结 4 1862
长情又很酷
长情又很酷 2020-12-20 22:55

How I can bind one of my buttons on control to X Button that closes the window ? I just want to create cancel button that just closes the window. I am using MVVM in my code.

4条回答
  •  鱼传尺愫
    2020-12-20 23:42

    MVVM solution without code-behind could also look like this:

    View:

    ViewModel:

    public ICommand CloseWindowCommand
    {
        get
        {
            return new RelayCommand(SystemCommands.CloseWindow);
        }
    }
    

    But SystemCommands is from .net-4.5 so if you rock in some older version of .net you can also use following.

    public ICommand CloseWindowCommand
    {
        get
        {
            return new RelayCommand((window) => window.Close());
        }
    }
    

提交回复
热议问题