Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mvvm

wpf

xaml

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. If possible to do it only in xaml, I just dont have any special code with the button click.

like image 415
Night Walker Avatar asked May 15 '13 20:05

Night Walker


2 Answers

You can just call the Close() method, which will close the window.

private void MyButton_Click(object s, RoutedEventArgs e)
{
    Close();
}
like image 185
qJake Avatar answered Oct 22 '22 18:10

qJake


If it's WPF (and provided I remember right) you can just use CallMethodAction from the parent as a behavior and utilize Close() method via just XAML. Something like;

Parent Window x:Name="window"

namespaces;

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

-

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

Hope this helps.

like image 22
Chris W. Avatar answered Oct 22 '22 16:10

Chris W.