How to separate behavior from UIElement in code for Silverlight?

In Silverlight 3.0, I added custom behavior for some UIElement in Code Behind.

I want to remove the behavior later at runtime.

What is the C # syntax for detaching an already added behavior from a UIElement ?

+4
source share
1 answer

I assume that you are talking about behavior derived from the Behavior<T> class in the Blend SDK ...

Do you still have a link to the behavior when you attached it?

 MyCustomBehavior myBehavior = new MyCustomBehavior(); myBehavior.Attach(myElement); ... myBehavior.Detach(); 

EDIT

If you no longer have a reference to the behavior instance when you want to separate it, you can do something like this to separate all the actions on the DependencyObject:

 foreach (var behavior in Interaction.GetBehaviors(myElement)) { behavior.Detach(); } 
+7
source

Source: https://habr.com/ru/post/1302320/


All Articles