backgroundTop
backgroundTop
Front Page
GreenBackgroundTop

Behaviors in Blend 3

How to create a simple Behavior for Blend 3

Behaviors are a new feature in Blend/Silverlight 3 and WPF. They allow designers to handle user interactions in XAML, and they also provide a clean division of concerns. Unfortunately, in the Blend 3 Preview, there aren't any behaviors included. This guide shows you how to write a behavior from scratch and also gives a look at a great new behavior written by Laurent Bugnion.

erikmork

2k Votes

GreenBackgroundBottom
 

Important Code

Mark as Inappropriate Content

This video is licensed under a Creative Commons License.

Creative Commons License Download the Video

Information From the Video

GreenBackgroundTop

Behaviors Solve a Problem

Probably the biggest design issue with Blend 2/Silverlight 2 is that it's difficult to hanlde user interactions from the XAML. This means that designers have to drop into code (or hand off to developers) to handle user events. The handling of a button's "Click" event is an example of this.
GreenBackgroundBottom
        public MainPage()
        {
            InitializeComponent();

            OurButton.Click += (s, e) =>
                {
                    //message box
                };
  
        }
 
GreenBackgroundTop

Blend 3 Preview doesn't have default behaviors

They can either be downloaded separately (see the references section) or they can be written from scratch.
GreenBackgroundBottom
 
GreenBackgroundTop

Adding a reference

In Blend 3 Preview, the .dll for Behaviors is stored in the Blend install directory (C:\Program Files\Microsoft Expression\Blend 3 Preview\Libraries)
GreenBackgroundBottom
 
GreenBackgroundTop

Create Behavior

Create a class that inherits from TargetedTriggerAction.
GreenBackgroundBottom
    public class PopupBehavior : Microsoft.Expression.Interactivity.TargetedTriggerAction<FrameworkElement>
    {

    }
 
GreenBackgroundTop

Adding Functionality

Override the Invoke method to customize what the Behavior will do. In this case, the Behavior will simply show a message box.
GreenBackgroundBottom
    public class PopupBehavior : Microsoft.Expression.Interactivity.TargetedTriggerAction<FrameworkElement>
    {


        protected override void Invoke(object parameter)
        {
            HtmlPage.Window.Alert(DisplayText);
        }
    }
 
GreenBackgroundTop

Adding Properties

Allowing a designer to cusomtize a Behavior is as simple as adding a dependency property. Here, there is a vanilla dependency property wrapped by a CLR type.
GreenBackgroundBottom
    public class PopupBehavior : Microsoft.Expression.Interactivity.TargetedTriggerAction<FrameworkElement>
    {

        public static readonly DependencyProperty DisplayTextProperty = 
            DependencyProperty.Register("DisplayText", typeof(string), typeof(PopupBehavior), null);

        [Category("DisplayText")]
        public string DisplayText
        {
            get { return (string)GetValue(DisplayTextProperty); }
            set { SetValue(DisplayTextProperty, value); }
        }

        protected override void Invoke(object parameter)
        {
            HtmlPage.Window.Alert(DisplayText);
        }
    }
 
GreenBackgroundTop

WPF Magnifying Behavior

Laurent Bugnion has created a really cool Behavior for WPF. It allows for the creation of a "magnifying" glass. Any element can both be magnified and also become a magnifying glass! Using this Behavior is super simple, and there's a link to his article and the code in the References section.
GreenBackgroundBottom
 
 
GreenBackgroundTop

References

Behaviors Post from a Silverlight MVP

Jose is a fellow Silverlight MVP and gives a great overview of Behaviors in Blend 3.

Mix09 Talk on Behaviors

This is a talk that covers Behaviors. There are some surprising things that Behaviors can accomplish. There's a link from here to some sample Behaviors that can be downloaded.

Interactivity Without Code

This is a great overview (no code) of the thinking behind Behaviors.

Laurent Bugnion's Magnifying Behavior

Laurent has implemented a WPF Magnifying Behavior. In this post, he provides the source and a sample application.

GreenBackgroundBottom
backgroundBottom
backgroundBottom