views:

10

answers:

1

Let's say I have a Silverlight user control with a textbox:

<TextBox>
  <ii:KeyTrigger Key="Enter">
      <!-- something here -->
  </ii:KeyTrigger>
</TextBox>

I inject my user control into root content control:

root.Content = new MyUserControl();

Now I know that KeyTrigger will be hooked up to TextBox when "Loaded" event of MyUserControl occurs:

AssociatedObject.KeyDown+=SomethingHere;

Where AssociatedObject will be a TextBox in this case.

The event is unsubscribed in OnDetaching method overload of KeyTrigger, the problem is that i dont know when OnDetaching is being called... Anyone could shed some light on this?

I have a button that injects a new instance of user control into root content:

root.Content = new MyUserControl();

everytime this happens "Loaded" event occurs on new instance and event for KeyTrigger is called, no OnDetaching is called for "old" user control instance however... won't this cause memory leaks? Is OnDetaching (Detach) called when a control dissapears from visual tree?

A: 

You want to handle the FrameworkElement.Closed event - OnDetached only gets called when the trigger is explicitly removed from its DependencyObject.

Michael S. Scherotter