mardi 6 mai 2014

c# - fermer la fenêtre sans code-behind dans WPF - Stack Overflow


Is it possible to bind a Button to Close the Window without adding a code-behind event?


<Button Content="OK" Command="{Binding CloseWithSomeKindOfTrick}" />

Instead of the following XAML:


<Button Content="OK" Margin="0,8,0,0" Click="Button_Click">

With the code-behind:


private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}

Thanks!




If you want close the dialog Window, you can add for Button IsCancel property:


<Button Name="CloseButton"
IsCancel="True" ... />

This means the following MSDN:



When you set the IsCancel property of a Button to true, you create a Button that is registered with the AccessKeyManager. The button is then activated when a user presses the ESC key.



Now, if you click on this Button, or press Esc then dialog Window is closing, but it does not work for the normal MainWindow.


To close the MainWindow, you can simply add a Click handler which has already been shown. But if you want a more elegant solution that would satisfy the MVVM style you can add the attached behavior:


public static class ButtonBehavior
{
#region Private Section

private static Window MainWindow = Application.Current.MainWindow;

#endregion

#region IsCloseProperty

public static readonly DependencyProperty IsCloseProperty;

public static void SetIsClose(DependencyObject DepObject, bool value)
{
DepObject.SetValue(IsCloseProperty, value);
}

public static bool GetIsClose(DependencyObject DepObject)
{
return (bool)DepObject.GetValue(IsCloseProperty);
}

static ButtonBehavior()
{
IsCloseProperty = DependencyProperty.RegisterAttached("IsClose",
typeof(bool),
typeof(ButtonBehavior),
new UIPropertyMetadata(false, IsCloseTurn));
}

#endregion

private static void IsCloseTurn(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue) == true)
{
if (MainWindow != null)
MainWindow.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);

var button = sender as Button;

if (button != null)
button.Click += new RoutedEventHandler(button_Click);
}
}

private static void button_Click(object sender, RoutedEventArgs e)
{
MainWindow.Close();
}

private static void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
MainWindow.Close();
}
}

And in MainWindow use this Behavior like as:


<Window x:Class="MyProjectNamespace.MainWindow" 
xmlns:local="clr-namespace:MyProjectNamespace">

<Button Name="CloseButton"
local:ButtonBehavior.IsClose="True" ... />


Is it possible to bind a Button to Close the Window without adding a code-behind event?


<Button Content="OK" Command="{Binding CloseWithSomeKindOfTrick}" />

Instead of the following XAML:


<Button Content="OK" Margin="0,8,0,0" Click="Button_Click">

With the code-behind:


private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}

Thanks!



If you want close the dialog Window, you can add for Button IsCancel property:


<Button Name="CloseButton"
IsCancel="True" ... />

This means the following MSDN:



When you set the IsCancel property of a Button to true, you create a Button that is registered with the AccessKeyManager. The button is then activated when a user presses the ESC key.



Now, if you click on this Button, or press Esc then dialog Window is closing, but it does not work for the normal MainWindow.


To close the MainWindow, you can simply add a Click handler which has already been shown. But if you want a more elegant solution that would satisfy the MVVM style you can add the attached behavior:


public static class ButtonBehavior
{
#region Private Section

private static Window MainWindow = Application.Current.MainWindow;

#endregion

#region IsCloseProperty

public static readonly DependencyProperty IsCloseProperty;

public static void SetIsClose(DependencyObject DepObject, bool value)
{
DepObject.SetValue(IsCloseProperty, value);
}

public static bool GetIsClose(DependencyObject DepObject)
{
return (bool)DepObject.GetValue(IsCloseProperty);
}

static ButtonBehavior()
{
IsCloseProperty = DependencyProperty.RegisterAttached("IsClose",
typeof(bool),
typeof(ButtonBehavior),
new UIPropertyMetadata(false, IsCloseTurn));
}

#endregion

private static void IsCloseTurn(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue) == true)
{
if (MainWindow != null)
MainWindow.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);

var button = sender as Button;

if (button != null)
button.Click += new RoutedEventHandler(button_Click);
}
}

private static void button_Click(object sender, RoutedEventArgs e)
{
MainWindow.Close();
}

private static void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
MainWindow.Close();
}
}

And in MainWindow use this Behavior like as:


<Window x:Class="MyProjectNamespace.MainWindow" 
xmlns:local="clr-namespace:MyProjectNamespace">

<Button Name="CloseButton"
local:ButtonBehavior.IsClose="True" ... />

Related Posts:

0 commentaires:

Enregistrer un commentaire