4
votes

I'm using the MahApps Metro window style, and I want to capture the event when the user clicks on the close button of the window.

I've set my ShutdownMode to OnExplicitShutdown so I need to call Application.Current.Shutdown(); when that button is clicked

How can I do this ?

4

4 Answers

1
votes

I believe that I am also trying to do the same thing as you (bind to the close window button) using WPF and MahApps.Metro. I wasn't able to find a way yet to bind to that command explicitly, but I was able to accomplish this by setting the ShowCloseButton property to false (to hide it) and then created my own close window command button and handled the logic in my viewmodel. Took me some digging, but I found that you can easily add your own window command controls in the command bar with MahApps.Metro just add similar markup to your XAML:

<Controls:MetroWindow.WindowCommands>
    <Controls:WindowCommands>
        <Button Content="X" Command="{Binding CancelCommand}" />
    </Controls:WindowCommands>
</Controls:MetroWindow.WindowCommands>
1
votes

You need to create a DependencyProperty to handle the close window behavior:

DependencyProperty

namespace MyApp.DependencyProperties
{
    public class WindowProperties
    {
        public static readonly DependencyProperty WindowClosingProperty =
           DependencyProperty.RegisterAttached("WindowClosing", typeof(RelayCommand), typeof(WindowProperties), new UIPropertyMetadata(null, WindowClosing));

        public static object GetWindowClosing(DependencyObject depObj)
        {
            return (RelayCommand)depObj.GetValue(WindowClosingProperty);
        }

        public static void SetWindowClosing(DependencyObject depObj, RelayCommand value)
        {
            depObj.SetValue(WindowClosingProperty, value);
        }

        private static void WindowClosing(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            var element = (Window)depObj;

            if (element != null)
                element.Closing += OnWindowClosing;

        }

        private static void OnWindowClosing(object sender, CancelEventArgs e)
        {
            RelayCommand command = (RelayCommand)GetWindowClosing((DependencyObject)sender);
            command.Execute((Window)sender);
        }
    }
}

In your ViewModel

public RelayCommand WindowClosedCommand { get; set; }

private void WindowClose()
{
    Application.Current.Shutdown();
}

In Constructor of ViewModel

this.WindowCloseCommand = new RelayCommand(WindowClose);

In your XAML

<mah:MetroWindow x:Class="MyApp.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:dp="clr-namespace:MyApp.DependencyProperties"
        dp:WindowProperties.WindowClosing="{Binding WindowClosedCommand}" />
1
votes

Since the solution from gotapps.net did't worked for me because I did't found how to programatically do that (My window does not have the Xaml file, it's just a base class). I found another workaround to use the same button to close the Window as follows:

internal class BaseWindow : MetroWindow
{
        public BaseWindow()
        {
            this.Loaded += BaseWindow_Loaded;
        }

        void BaseWindow_Loaded(object sender, EventArgs e)
        {
            Button close = this.FindChild<Button>("PART_Close");
            close.Click += close_Click;
        }

        void close_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }
}
0
votes

You can use "Closing" or "Closed" event

Closing handler gets triggered when user clicks on the Close button. This also gives you control on whether the application should be closed.

Similarly, Closed handler gets triggered just before the window is closed

<Controls:MetroWindow x:Class="MyClass.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="My Class"
Closing="MainWindow_OnClosing">
</Controls:MetroWindow>