How to separate behavior from uielement behind the Silverlight code?

In Silverlight 3.0, I added custom behaviors for some UIElements in Code Behind.

I want to remove the behaviors at runtime later .

What is the C# syntax to separate the added behavior from the UIElement?

I guess you’re talking about a behavior that originates from behavior . In Blend SDK Class…

Can you still refer to the behavior when attached?

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

Edit

If you no longer refer to the behavior instance when you want to separate the behavior, you can do the following to separate all the behaviors on the DependencyObject:

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

In Silverlight 3.0, I added custom behaviors for some UIElements in Code Behind.

I want to remove the behaviors at runtime later.

From UIElement What is the C# syntax to separate the added behavior?

I guess you are talking about a behavior derived from behavior . The classes in the Blend SDK…

Can you still refer to the behavior when attaching?

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

Edit

If you no longer refer to the behavior instance when you want to separate the behavior, you can do the following to separate all the behaviors on the DependencyObject:

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

Leave a Comment

Your email address will not be published.