Handle closing event of all windows in wpf










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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













0












0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












2 Answers
2






active

oldest

votes


















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>





share|improve this answer

























  • 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


















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( ... ));





share|improve this answer























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

    – n3verLieMe
    Nov 12 '18 at 11:23










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
);



);













draft saved

draft discarded


















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









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>





share|improve this answer

























  • 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















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>





share|improve this answer

























  • 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













1












1








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>





share|improve this answer















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>






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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













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( ... ));





share|improve this answer























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

    – n3verLieMe
    Nov 12 '18 at 11:23















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( ... ));





share|improve this answer























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

    – n3verLieMe
    Nov 12 '18 at 11:23













0












0








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( ... ));





share|improve this answer













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( ... ));






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ