This is a mirror of official site: http://jasper-net.blogspot.com/

Behaviors and Triggers in Silverlight

| Monday, June 27, 2011
1. Introduction

With the release of Silverlight 3, a lot of new cool features have been introduced. One of my favorite definitely is the support of behaviors and triggers. In WPF the triggers are extremely powerful. They allow you to declaratively associate an action with an event or property value. In the previous versions of Silverlight one of the things that were really missing were the triggers and the behaviors. It was not possible, for example, to add mouse-overs to objects declaratively (as in WPF). Currently it is still not possible, you have to write procedural code, but at least that code is separated, so the designer doesn’t have to write it or understand it. In that post I will focus exactly on that new feature in Silverlight and will show you how to create your own custom behaviors and triggers.

You can find the demos in the end of each section.

Download Source
2. Prerequisites

In order to write behaviors and triggers you need to have installed the Microsoft Expression Blend SDK on your machine. After that you need to add a reference to the System.Windows.Interactivity.dll assembly which is located in:

{Program Files}\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\Silverlight

I don’t know why but this assembly currently is not part of the Silverlight SDK. You can download the Expression Blend product from here.
3. Behavior types

Currently you can use three types of bevahors: Behavour, TriggerAction and TargetedTriggerAction. The class diagram is shown on the next figure:

BehaviorClasses.png

4. Using the Behavior<T> class

For simple scenarios the generic Behavior<T> class is excellent choice. This class has only two overridable methods which notify the current behavior when an object is attached and detached from it. For my post I will create two behaviors: the first one just inverts the color of an image, when the image is clicked, and the second is little more complicated – it adds an animation and creates a magnifier when the mouse is over the image.

The first thing you should do when creating behaviors is to create a new class which inherits from the generic class Behavior<T>, where T is a dependency object. In the most cases you will inherit from the Behavior<FrameworkElement> or Behavior<UIElement>.

public class InverseColorClickBehavior :
    Behavior<FrameworkElement>
{
    public InverseColorClickBehavior() :
        base()
    {
    }
}

For that particular case I am interested in the mouse click event. That’s why in the OnAttached method I will attach to the MouseLeftButtonDown event of the associated with that behavior object. And respectively in the OnDetaching method I will detach from that event.

protected override void OnAttached()
{
    base.OnAttached();
    this.AssociatedObject.MouseLeftButtonDown +=
        new MouseButtonEventHandler( AssociatedObject_MouseLeftButtonDown );
}

 

protected override void OnDetaching()
{
    base.OnDetaching();
    this.AssociatedObject.MouseLeftButtonDown -=
        new MouseButtonEventHandler( AssociatedObject_MouseLeftButtonDown );

Read more: Silverlight Show
QR: Behaviors-and-Triggers-in-Silverlight-3.aspx

Posted via email from Jasper-net

0 comments: