Strategy for unsubscribing event handlers

Posted by stiank81 on Stack Overflow See other posts from Stack Overflow or by stiank81
Published on 2010-03-23T14:17:43Z Indexed on 2010/03/23 14:23 UTC
Read the original article Hit count: 343

In my WPF application I have a View that is given a ViewModel, and when given this View it adds event handlers to the ViewModel's PropertyChanged event. When some action occur in the GUI I remove the View and add another View to the holding container - where this new one is bound to the same ViewModel.

After this has happened the old View still keeps handling PropertyChanged events in the ViewModel. I'm assuming this happens because the View hasn't been collected by the Garbage Collector yet, and therefore is alive? Well - I need it to stop. My assumption is that I need to manually detach the event handler from the ViewModel? Is there a best-practice on how to handle this?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wpf