I'm quit looking for a while and can't find a solution for this supposed easy task. I am using two events via event triggers, the 'Activated' and the 'Closed' event.
I'm using the 'Activated' event to update my Window, but I don't want it be fired when I close the application from the taskbar. Any ideas?
My program contains the following eventtrigger in the XAML-Code:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Activated" >
<i:InvokeCommandAction Command="{Binding WindowActivated}" />
</i:EventTrigger>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding ClosingCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
To my knowledge this isn't possible. And it's got nothing to do with WPF or MVVM, it's how Windows handles this itself. When you right-click on the taskbar you force the focus away from your application, so you'll get a deactivate event. Selecting close from the pop-up menu causes windows to send a WM_SYSCOMMAND/SC_CLOSE but it has to activate your window first, and there's no way at the application level to know the future intention of the system thread that's initiating this action.
As I see it you have 2 choices. The first is to use a DispatcherTimer to add a small delay between the Activate and the update of your window, long enough for the subsequent close message to arrive.
The second is to do your update in a CompositionTarget.Rendering event handler. This event is triggered even when the application doesn't have focus (seomthing you may want to track yourself if you don't want background updates), but the SC_CLOSE event that follows the WM_ACTIVATE event should occur fast enough for it to arrive before the next CompositionTarget.Rendering event, at which point you know your application is closing and can therefore ignore all subsequent updates.