开发者

When IAttachedObject.Detach() (KeyTrigger.Detaching()) method is actually called?

开发者 https://www.devze.com 2023-01-13 03:29 出处:网络
Let\'s say I have a Silverlight user control with a textbox: <TextBox> <ii:KeyTrigger Key=\"Enter\">

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 开发者_JS百科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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消