MahApps Metro Macro Close Event

I am using the Metro MahApps window style and I want to capture an event when the user clicks the close window button.

I set my ShutdownMode to OnExplicitShutdown, so I need to call Application.Current.Shutdown();when this button is pressed

How can i do this?

+4
source share
3 answers

I believe that I am also trying to do the same thing as you (bind to the button to close the window) using WPF and MahApps.Metro. I was not able to find a way to link this command explicitly, but I was able to do this by setting the ShowCloseButton property to false (to hide it), and then created my own window close command button and processed the logic in my viewmodel. It took a bit of digging, but I found that you can easily add your own command-line window controls on the command line using 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
source

You need to create a DependencyProperty to handle the behavior of the closing window:

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 the 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

Since the solution from gotapps.net did not work for me, because I did not find how to do it programmatically (there is no Xaml file in my window, it is 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);
        }
}
+1
source

All Articles