OnDetaching function of behavior is not called

The OnAttached is called when XAML parser parses XAML and creates an instance of a behaviour that adds to the BehaviorCollection of the target control which is exposed as DependencyAttached property.

However when if the view is disposed, the collection (Behavior collection) was disposed of, it will never trigger OnDetaching method.

If the behaviour is not properly cleanup it will not be collected by GC and will also hold BehaviorCollection and other behaviors in that collection. The behaviours are designed to extend AssociatedObject, as long as you are subscribing to AssociatedObject events its fine as the AssociatedObject (publisher) will die and your behaviour will be collected by garbage collector.

A good source.


An alternative is to handle the "Closing" event (when a user clicks the upper right 'X' button) of the window, and OnDetaching there.

 <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <cmd:EventToCommand Command="{Binding CloseCommand}" />
        </i:EventTrigger>
 </i:Interaction.Triggers>

Then associate the handler in the View constructor:

MyWindow() 
{
    // Set up ViewModel, assign to DataContext etc.
    Closing += viewModel.OnWindowClosing;
}

And add the handler to the ViewModel:

public void OnWindowClosing(object sender, CancelEventArgs e) 
{
   // Cancel, OnDetaching, etc
}

Try to subscribe to AssociatedObject.Unloaded event and inside the eventHander unsubscribe to all the mouse events. Behaviors OnDetaching() func. not always invoked at "time".