1
votes

I'm using a DataTemplate to apply a View to a ViewModel. I have a case where when a certain thing happens in one instance of the View (DataTemplate), I need to take an action in all other instances.

I'm already accomplishing this by implementing the Initialized event on one of the controls in the DataTemplate, and using that event to add a reference to the control to a list in the codebehind. This is working, since there's only one codebehind for the DataTemplate (in a resource dictionary), they can all access the same list.

I'm a bit worried though, since instances of the DataTemplate get created and destroyed. Am I not keeping extra references around to old instances of the DataTemplate that are no longer necessary? Is there some way I can clean them up? Is there a corresponding event... the opposite of Initialized... when a control or DataTemplate is gone?

1

1 Answers

0
votes

Would it be feasible for you to model this interaction directly in the ViewModel? Perhaps with a property or event on the item(s) being data-bound? That way your view only has to decide the behavior/presentation of the event.