views:

16

answers:

1

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?

A: 

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.

Daniel Pratt
I was thinking that too, but then I'm just pushing the same problem to the ViewModel. Then I have to track all instances of the ViewModel, which is kind of the same thing. But I agree it might be cleaner.
Scott Whitlock