How to bind Close command to a button

前端 未结 7 1263
时光说笑
时光说笑 2020-11-30 19:09

The easiest way is to implement ButtonClick event handler and invoke Window.Close() method, but how doing this through a Command bindi

7条回答
  •  囚心锁ツ
    2020-11-30 19:37

    For .NET 4.5 SystemCommands class will do the trick (.NET 4.0 users can use WPF Shell Extension google - Microsoft.Windows.Shell or Nicholas Solution).

        
            
        
        
        

    In the Code Behind you can implement the handlers like this:

        private void CloseWindow_CanExec(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    
        private void CloseWindow_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            SystemCommands.CloseWindow(this);
        }
    

提交回复
热议问题