Behavior in SILVERLIGHT 3.0
This article will give a basic introduction of BEHAVIOR feature in SILVERLIGHT3.0. I will also walkthrough to create a custom behavior.
What is Behavior in SILVERLIGHT 3?
According to definition given in the product introduction of SILVERLIGHT3
A Behavior is in essence a reusable piece of interactivity that can be applied directly to user interface elements in Expression Blend.
The whole product introduction on behavior can be read here.
I summarized few of the points as below about SILVERLIGHT behavior
1. Behavior is new feature introduced in SILVERLIGHT 3.0.
2. Behavior is a reusable piece of code, which can be attached to any object declaratively.
3. Behavior is the way to allow designer to add functionality to XAML elements without codes.
4. Behavior allows attaching functionality to an object without writing any code.
5. Behavior encapsulates the functionality and can be attached to any element.
6. Behavior can be attach to an element or object through XAML. There is no need to write C# code.
7. A single behavior can be attached to any number of objects.
8. Behavior is reusable piece of code can be attached to any object.
9. Behavior comes with System.Windows.Interactivity.dll
Creating a Custom Behavior
Create a SILVERLIGHT Application. Two projects will get created SilverLightApplication1 and SilverLightApplication1.Web. If you are not, changing the default name. Add reference of System.Windows.Interactivity.dll in SilverLightApplication1 project.
Right click on SILVERLIGHT project and add a class. Give any name, for my purpose; I am giving name ImageBehavior.
a. Add the namespace using System.Windows.Interactivity;
b. Inherit class from Behavior
c. Make T as DependencyObject
d. Override the methods OnAttached and OnDetaching
e. On MouseEnter event and MouseLeave event write the simple logic to rotate the object to which Behavior will be attached.
public class ImageBehavior : Behavior
protected override void OnAttached()
var ad = this.AssociatedObject as FrameworkElement;
ad.MouseEnter += (sender, args) =>
var p = new PlaneProjection();
p.RotationZ = new Random().Next();
ad.Projection = p;
ad.MouseLeave += (sender, args) =>
ad.Projection = null;
protected override void OnDetaching()
}Attaching Custom Behavior to object
Before adding custom behavior to element, we need to add namespaces. Open the XAML page and add below namespaces.
First namespace is for the System.Windows.Interactivity
dll and second one is namespace of the custom behavior class.
Since; I am going to attach the custom behavior we created with Image control. So for the source of Image control add an image to SILVERLIGHT project by Add an existing item. I have added an image and gave the name a.jpg.
Add an Image control on XAML and attach the behavior like below.
isnamespace of dll and behavior is namespace of custom behavior class. MainPage.Xaml
There is no need of do any coding in MainPage.Xaml.cs
Run the application
On mouse enter to Image; Image will rotate with random degree of angle. Like below. Thanks for reading.