David Beck
David Beck

Reputation: 297

Binding to Window Events in XAML

I have read several posts on how to get the window settngs from the property and bind them "twoway" in the Window XAML when using the MVVM model. But, invariably, the author ends up saving them in the code behind as

void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    Properties.Settings.Default.Save(); 
} 

I would like to set the MainWindow XAML to bind to a class routine that saves the Properties. I would think there should be someting like:

WindowClosing="{Binding MyClosingEventRoutine}"

Upvotes: 0

Views: 3168

Answers (2)

blindmeis
blindmeis

Reputation: 22435

you can use the EventTrigger behavior from blend SDK or MVVM Light. on Window closing your command in your viewmodel is called.

<Window xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing" >
        <i:InvokeCommandAction Command="{Binding MyClosingEventRoutine}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
</Window>

Upvotes: 4

Nogusta
Nogusta

Reputation: 919

Hard to tell from your post but I'm guessing you are doing MVVM, seeing as you don't like the old code behind? If so you can just grab some of the code from the MVVM frameworks out there that do events to commands. This will then allow you to bind the windows closing event to a command in your viewmodel as per the following link

Upvotes: 0

Related Questions