How to associate the Close command with a button

The easiest way is to implement the ButtonClick event ButtonClick and call the Window.Close() method, but how to do this using the Command binding?

+52
button wpf wpf-controls commandbinding
Jun 30 '09 at 20:32
source share
6 answers

I think in real-world scenarios, a simple click handler is probably better than overly complex command systems, but you can do something like this:

using the RelayCommand from this article http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

 public class MyCommands { public static readonly ICommand CloseCommand = new RelayCommand( o => ((Window)o).Close() ); } <Button Content="Close Window" Command="{X:Static local:MyCommands.CloseCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/> 
+44
Jul 01 '09 at 14:18
source share

All that is required is a bit of XAML ...

 <Window x:Class="WCSamples.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Close" Executed="CloseCommandHandler"/> </Window.CommandBindings> <StackPanel Name="MainStackPanel"> <Button Command="ApplicationCommands.Close" Content="Close Window" /> </StackPanel> </Window> 

And a little C # ...

 private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e) { this.Close(); } 

(adapted from this MSDN article )

+65
Jun 30 '09 at 21:02
source share

Actually, this is possible without C # code. The key is to use interactions:

 <Button Content="Close"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <ei:CallMethodAction TargetObject="{Binding ElementName=window}" MethodName="Close"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> 

For this to work, just set the x:Name your window to the “window” and add these two namespaces:

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 

This requires that you add the Expression Blend SDK DLL to your project, specifically Microsoft.Expression.Interactions .

If you do not have Blend, the SDK can be downloaded here .

+53
Apr 12 2018-11-11T00:
source share

The simplest solution that I know of is to set the IsCancel property to true to close the Button :

 <Button Content="Close" IsCancel="True" /> 

No bindings required, WPF will do this automatically!

Link: MSDN Property Button.IsCancel .

+30
Oct 31 '14 at 16:25
source share

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

  <Window.CommandBindings> <CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}" CanExecute="CloseWindow_CanExec" Executed="CloseWindow_Exec" /> </Window.CommandBindings> <!-- Binding Close Command to the button control --> <Button ToolTip="Close Window" Content="Close" Command="{x:Static SystemCommands.RestoreWindowCommand}"/> 

In Code Behind, you can implement handlers as follows:

  private void CloseWindow_CanExec(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void CloseWindow_Exec(object sender, ExecutedRoutedEventArgs e) { SystemCommands.CloseWindow(this); } 
+2
Apr 15 '15 at 16:53
source share

At the beginning, it was also a little difficult for me to understand how this works, so I wanted to publish a better explanation of what is actually happening.

According to my research, the best way to deal with such things is to use Command Bindings. What happens is the “Message”, transmitted to everyone in the program. So you need to use CommandBinding . Essentially, it says, "When you hear this message, do it."

So, in the question, the user is trying to close the window. The first thing we need to do is configure our functions, which will be called when the SystemCommand.CloseWindowCommand broadcasts. If desired, you can assign a function that determines whether the command should be executed. An example would be closing a form and checking if the user has saved.

MainWindow.xaml.cs (or other code)

 void CloseApp( object target, ExecutedRoutedEventArgs e ) { /*** Code to check for State before Closing ***/ this.Close(); } void CloseAppCanExecute( object sender, CanExecuteRoutedEventArgs e ) { /*** Logic to Determine if it is safe to Close the Window ***/ e.CanExecute = true; } 

Now we need to configure the “Connection” between SystemCommands.CloseWindowCommand and CloseApp and CloseAppCanExecute

MainWindow.xaml (or anything that implements CommandBindings)

 <Window.CommandBindings> <CommandBinding Command="SystemCommands.CloseWindowCommand" Executed="CloseApp" CanExecute="CloseAppCanExecute"/> </Window.CommandBindings> 

You can omit CanExecute if you know that the command should always be executed. Save may be a good example depending on the application. Here is an example:

 <Window.CommandBindings> <CommandBinding Command="SystemCommands.CloseWindowCommand" Executed="CloseApp"/> </Window.CommandBindings> 

Finally, you must tell UIElement to send the CloseWindowCommand command.

 <Button Command="SystemCommands.CloseWindowCommand"> 

This is actually a very simple thing, just make the connection between the Command and the actual function Execute, then tell Control to send the command to the rest of your program, saying: "Ok, everyone performs your functions for the CloseWindowCommand command."

Actually, this is a very good way to pass this, because you can fully use the executable function without having a shell like you, for example, using WinForms (using ClickEvent and calling a function in an event function), for example:

 protected override void OnClick(EventArgs e){ /*** Function to Execute ***/ } 

In WPF, you attach a function to a command and tell UIElement to execute the function attached to the command.

Hope this clarifies the situation ...

+1
Feb 23 '17 at 14:12
source share



All Articles