silverlight attach behavior in code
October 27, 2010 at 9:51 PM
—
dsoltesz
I had a scenario where I needed to build part of the UI completely from Code and wanted to include a behavior as part of one of the UI controls.
Lets walk through and example of how to do this.
I want to create a WrapPanel that has the FluidMoveBehavior associated to the WrapPanel and applies the behavior to all its children.
var panel = new WrapPanel
{
HorizontalAlignment = HorizontalAlignment.Left
};
var timeSpan = new TimeSpan(0, 0, 0, 0, 400);
var ease = new CubicEase
{
EasingMode = EasingMode.EaseIn
};
var fluidMoveBehavior = new FluidMoveBehavior
{
AppliesTo = FluidMoveScope.Children,
Duration = new Duration(timeSpan),
EaseX = ease,
EaseY = ease
};
System.Windows.Interactivity.Interaction.GetBehaviors(panel).Add(fluidMoveBehavior);
Pretty easy, just make sure you add references to
- System.Windows.Interactivity
- Microsoft.Expression.Effects
- Microsoft.Express.Interactions