Binding UserControl unload event to ViewModel

2019-04-16 10:37发布

问题:

I'm reasonable new to MVVM and have a usercontrol as my View. I'd like to Bind the "unload" event of that control to a method in my ViewModel. To clean up stuff when the control is shutting down.

My search so far has only come up with xamples that require you to include some kind of toolkit or libraries from expression blend SDK. I just want to use he generic .net 4.1 libraries.

Can some one show me how to accomplish this ?

回答1:

I've always used the code shown here to create an AttachedCommandBehavior. It doesn't require any extra libraries and can be used like this:

<local:MyControl 
    local:CommandBehavior.Event="Unloaded" 
    local:CommandBehavior.Command="{Binding ViewModelUnloadedCommand}" />