silverlight attach behavior in code

October 27, 2010 at 9:51 PMdsoltesz

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

Posted in: .NET | behaviors | c# | silverlight

Tags: , ,

Comments (1) -

Useful trick, thanks for sharing.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading