Can I apply Blend behavior with a style or template?

I am very pleased with my small collection of Blend behavior, actions and triggers - they are easy to use and powerful. But I still cannot figure out how to avoid applying them based on each element. For example, if I have a behavior as such:

<Rectangle> <i:Interaction.Behaviors> <il:MouseDragElementBehavior/> </i:Interaction.Behaviors> </Rectangle> 

and I have several draggable rectangles in my window, each of which should have an overloaded label. I would like to write something like this:

 <Style x:Key="RectangleStyle" TargetType="{x:Type Rectangle}"> <Setter Property="i:Interaction.Behaviors" Value="il:MouseDragElementBehavior"/> </Style> 

It could be a style, template, or some other way to avoid repeating the markup of a behavior or action. The best solution I've come up with so far is to create a special behavior for the container (when attached, it lists the children associated with the child events). Any ideas?

+6
wpf xaml expression-blend
source share
3 answers

I ran into the same problem, and I posted on my blog about how to create an attached property to get around this drawback of the Blend SDK.

+3
source share

I have not tried, but what I will probably do is create an attached property that may contain a collection of Behaviors. In the property of the changed property of this property, I would list the collection and add each of these actions to the actual Interation.Behaviors property. This is a bit dirty, but it should work.

Update

This approach will not work without additional work: the problem is that Behaviors and Triggers can only be bound to one object at a time. Probably, therefore, in the first place, there is a restriction on their use using style. To make this work, you need to create some kind of trigger or factory behavior.

+1
source share

You can create your own class that inherits from Rectangle and apply the behavior there.

0
source share

All Articles