Dmitriyz
Dmitriyz

Reputation: 148

How to access the behavior that was attached to a UI element?

For example, I have this:

Border myBorder = new Border();
...
designArea.Children.Add(myBorder);

ResizeAndMoveBehavior b = new ResizeAndMoveBehavior();
b.CurrentProperties = thisButtonProperties;
b.Attach(myBorder);

Now when I get myBorder from the designArea's children how can I access the CurrentProperties of the behavior attached to myBorder?

Upvotes: 1

Views: 520

Answers (1)

Sander
Sander

Reputation: 26374

You are attaching your behavior in a non-standard way, which means that there is no way to get the attached behavior(s) back later. However, if you do it the standard way, you can get this info. Here is an example of how behaviors should be managed.

using System.Windows.Interactivity;

// Add a behavior.
Interaction.GetBehaviors(myBorder).Add(b);

// Get all behaviors of an object.
BehaviorCollection behaviors = Interaction.GetBehaviors(myBorder);

// Get a specific type of behavior.
ResizeAndMoveBehavior myBehavior = (ResizeAndMoveBehavior)behaviors
    .Where(b => b is ResizeAndMoveBehavior).Single();

Upvotes: 2

Related Questions