Handle closing event of all windows in wpf
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
c# .net wpf events eventhandler
add a comment |
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
c# .net wpf events eventhandler
Are you looking for something like this : stackoverflow.com/questions/8969846/…?
– user3164323
Nov 12 '18 at 11:41
@user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window
– n3verLieMe
Nov 12 '18 at 13:07
add a comment |
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
c# .net wpf events eventhandler
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
c# .net wpf events eventhandler
c# .net wpf events eventhandler
edited Nov 12 '18 at 13:10
n3verLieMe
asked Nov 12 '18 at 9:22
n3verLieMen3verLieMe
11310
11310
Are you looking for something like this : stackoverflow.com/questions/8969846/…?
– user3164323
Nov 12 '18 at 11:41
@user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window
– n3verLieMe
Nov 12 '18 at 13:07
add a comment |
Are you looking for something like this : stackoverflow.com/questions/8969846/…?
– user3164323
Nov 12 '18 at 11:41
@user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window
– n3verLieMe
Nov 12 '18 at 13:07
Are you looking for something like this : stackoverflow.com/questions/8969846/…?
– user3164323
Nov 12 '18 at 11:41
Are you looking for something like this : stackoverflow.com/questions/8969846/…?
– user3164323
Nov 12 '18 at 11:41
@user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window
– n3verLieMe
Nov 12 '18 at 13:07
@user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window
– n3verLieMe
Nov 12 '18 at 13:07
add a comment |
2 Answers
2
active
oldest
votes
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>
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
– n3verLieMe
Nov 12 '18 at 15:06
1
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.
– Rob
Nov 12 '18 at 15:08
add a comment |
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( ... ));
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259113%2fhandle-closing-event-of-all-windows-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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>
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
– n3verLieMe
Nov 12 '18 at 15:06
1
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.
– Rob
Nov 12 '18 at 15:08
add a comment |
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>
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
– n3verLieMe
Nov 12 '18 at 15:06
1
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.
– Rob
Nov 12 '18 at 15:08
add a comment |
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>
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>
edited Nov 12 '18 at 15:35
answered Nov 12 '18 at 14:14
RobRob
1,0481123
1,0481123
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
– n3verLieMe
Nov 12 '18 at 15:06
1
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.
– Rob
Nov 12 '18 at 15:08
add a comment |
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
– n3verLieMe
Nov 12 '18 at 15:06
1
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.
– Rob
Nov 12 '18 at 15:08
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
– n3verLieMe
Nov 12 '18 at 15:06
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
– n3verLieMe
Nov 12 '18 at 15:06
1
1
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.
– Rob
Nov 12 '18 at 15:08
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.
– Rob
Nov 12 '18 at 15:08
add a comment |
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( ... ));
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
add a comment |
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( ... ));
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
add a comment |
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( ... ));
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( ... ));
answered Nov 12 '18 at 11:21
pjrkipjrki
168113
168113
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
add a comment |
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
unfortunately I can't use unload event because I want to cancel close in closing event
– n3verLieMe
Nov 12 '18 at 11:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259113%2fhandle-closing-event-of-all-windows-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you looking for something like this : stackoverflow.com/questions/8969846/…?
– user3164323
Nov 12 '18 at 11:41
@user3164323 Actually I'm looking for something which catches closing of all windows not just controls inside one window
– n3verLieMe
Nov 12 '18 at 13:07