I have a PRISM WPF application; and I have a PatientViewModel that in its constructor subscribes to an event (let's say CultureChangedEvent), and executes an action.

In my main view model I have an ObservableCollection.

If the user removes a PatientViewModel instance from the ObservableCollection, and then a CultureChangedEvent is fired, the removed PatientViewModel is still in memory and receives the event. So currently, when removing a PatientViewModel, I unsubscribe from the event; but I wonder of this is the correct approach? Or am I missing something?

1 Answer
1

Even though an object is removed from an observable collection it still exists (thus it still subcribes to the event). The way you are handling this is an acceptable solution. Another one is to expose your ObservableCollection as a ReadOnlyObservableCollection and then provide your own add and remove functions. Inside your Remove function unsubscribe from the event and then remove the item from your private "normal" ObservableCollection. Thus you can unsubscribe from the event before you remove the item.