I'm stumbling my way through my first WPF desktop application using C# and am trying to stick to good programming practice by not repeating code. I've come a little unstuck when trying to add an event handler to buttons in different windows.
I have two windows (named 'MainWindow' and 'ViewContent') which both contain buttons to exit the application.
The buttons are both identical in XAML, and are created in separate windows:
<Button Click="Exit_Application" />
The event handler for a button click will then run:
public void Exit_Application(object sender, RoutedEventArgs e)
{
// Exit the application
System.Windows.Application.Current.Shutdown();
}
This works when I include the 'Exit_Applicaiton' method in the code-behind for both windows, but I was hoping to only have to include this method once and be able to use it globally. I've searched around and can't seem to find any information on using click event handlers globally, Is this possible?
Any help would be greatly appreciated.