0

In WPF for registering an event for all window something like this should written in App class :

EventManager.RegisterClassHandler(typeof(Window), Window.PreviewMouseDownEvent, new MouseButtonEventHandler(OnPreviewMouseDown)); 

But Window class does not have any property for handling Closing event

2
  • Are you looking for something like this : stackoverflow.com/questions/8969846/…? Commented Nov 12, 2018 at 11:41
  • @user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window Commented Nov 12, 2018 at 13:07

2 Answers 2

1

Window does have a Closing event that you can cancel but it is not RoutedEvent so you cannot subscribe to it in this fashion.

You can always inherit the Window and subscribe to closing in one place. All inheriting Windows will inherit this behavior also.

EDIT

This can be done with behaviors as well. Make sure you install a NuGet package called Expression.Blend.Sdk. Than create an attached behavior like so:

using System.Windows; using System.Windows.Interactivity; namespace testtestz { public class ClosingBehavior : Behavior<Window> { protected override void OnAttached() { AssociatedObject.Closing += AssociatedObject_Closing; } protected override void OnDetaching() { AssociatedObject.Closing -= AssociatedObject_Closing; } private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = MessageBox.Show("Close the window?", AssociatedObject.Title, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel; } } } 

Than in your XAML add this behavior like so:

<Window x:Class="testtestz.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" xmlns:local="clr-namespace:testtestz" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"> <i:Interaction.Behaviors> <local:ClosingBehavior/> </i:Interaction.Behaviors> <Grid> </Grid> </Window> 
Sign up to request clarification or add additional context in comments.

2 Comments

Although inheritance seems a descend way but I hate to do so for visualized classes (Like Window, button and so) cause overhead of inheritance can be sensed in UX specially when user does not have a normal GPU
I hate this too since visual inheritance does not exist in XAML. The only other thing I can think of is an attached behavior that you would attach to every Window.
0

What about registering to the Unloaded event? which has it's own property. For example:

EventManager.RegisterClassHandler(typeof(Window), PreviewMouseDownEvent, new MouseButtonEventHandler(OnPreviewMouseDown)); EventManager.RegisterClassHandler(typeof(Window), UnloadedEvent, new RoutedEventArgs( ... )); 

1 Comment

unfortunately I can't use unload event because I want to cancel close in closing event

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.