The objective of this article series is to give a quick overview of Behaviors, Triggers and Actions in Silverlight and WPF. Together, they enable a great deal of design time interactivity for your UI. They also make possible re-use and re-distribution of interaction logic. This is the first article in the series, and I’ll explain about Behaviors and creating custom behaviors.
- Part I - Behaviors, Triggers and Actions in Silverlight And WPF Made Simple – Behaviors (this one)
- Part II - Behaviors, Triggers and Actions in Silverlight And WPF Made Simple – Triggers
Note: You need Expression Blend 4.0
Note: Why this post? After publishing my Silverlight and WPF interaction frameworks, Silverlight Experimental Hacks (Slex) and WPF Experimental Hacks (Wex), I got a number of requests from the community to write few simple posts on these topics, introducing the basic concepts. So here we go.
Behaviors – Scratching the Surface
A behavior is something you attach to an element, modifying the way how the element should present itself, or how the element should respond to user interactions. First, Let us add an existing behavior to an object to see how behaviors work. Later we’ll create a custom behavior and may use the same from Blend.
Step 1. Let us start with a simple Silverlight application. Fire up Expression Blend 4.0 beta, and click File->New to select a Silverlight 4.0 application, and click OK.
Step 2. Once you have the project created, just add a rectangle to your MainPage.xaml canvas, and give it a nice back ground color using the color picker (I gave blue:)). Also, at this point, click View->Active Document View->Split View – So that you can view the Xaml along with the designer. You may also press F5 and run the project if you want, and make sure you can’t drag the rectangle around as of now ;).
Step 3. Now, our objective is to attach a dragging behavior to our rectangle, so that it can be dragged around by the user in the screen. For this, we’ll attach the MouseDragElementBehaviour to our rectangle. To do this, go to the Assets tab in Blend (See the arrow below), and Click the Behaviors label. Now, from the right pane of the Assets tab, drag and drop MouseDragElementBehaviour to your rectangle. Press F5 and run the project, and you see that you’ll be able to drag your element any where.
Step 4. Let us take a step back here, and have a look at the XAML. You’ll see the MouseDragElementBehaviour added to the Behaviors attached property of our dumb little rectangle.
Behind the scenes
The System.Windows.Interactivity infrastructure (initially introduced in Blend 3) simplifies the creation of Behaviors, triggers and actions.
The System.Windows.Interactivity namespace has multiple classes to inherit from and to create your own Behavours, triggers and actions. The WPF and Silverlight versions are present at C:\Program Files\Microsoft SDKs\Expression\YourBlendVersion\Interactivity\Libraries. In the same folder, you’ll also find the Microsoft.ExpressionBlend.Interactions.dll which hosts various concrete behaviours and triggers, like the MouseDragElementBehaviour we just applied.
If you have a look at the MouseDragElementBehavior using Reflector, you’ll find that MouseDragElementBehaviour is inherited from the Behaviour<T> abstract class in System.Windows.Interactivity.
Creating your own Behaviors
So, let us create a custom behavior.
Step 1 – Create a Behavior. In Blend, right click your project in the Projects pane, and click ‘Add New Item’, to bring up the New Item dialog box. Select Behavior from there, give a nice name and click OK.
Step 2 – See what is inside. Have a look at the New behavior class that got added to your project. You’ll find that our Behavior class is inherited from Behavior<T> in System.Windows.Interactivity as discussed earlier. Also, have a look at the override methods, OnAttached and OnDetaching (and the comments below them).
Step 3 – Add some meat. And what we should do in the Behavior? Let us tilt the object a bit, when the mouse moves over an object with this Behavior applied. Just modify OnAttached to have the following code. The AssociatedObject property of the Behavior will provide the current object context, to which this behavior is applied. Let us hook the MouseEnter and MouseLeave events, so that we’ll apply a simple projection to the object when the mouse moves over, and reset the project when the mouse leaves the object.
Step 4 – Rebuild. Now, click Project->Build Project in Expression Blend menu. And goto the Assets window again and click Behaviors to view MyBehavior there. Just drag and drop the behavior to your object. See that the new behavior is added to our Rectangle in Xaml as well.
Step 5 – Run. You are done. Press F5 and run your project, and see that when you move your mouse over the rectangle to start the dragging, the rectangle is getting a bit tilted.