This post is the second in a series of posts about the Behaviors model in Blend 3. In this post, we’ll talk about some of the important concepts of Behaviors in Blend 3. The next posts in the series will bring some samples.
For introductory information on Behaviors the motivation for them, please see my previous post here.
Triggers, Actions, Behaviors
The Behaviors mechanism in Blend 3 is built around three important related concepts for building interactivity: Triggers, Actions and Behaviors.
If you are familiar with WPF, Triggers and Actions are not really new to you: We use exactly the same model, with a few important additions:
- Triggers and Actions in Blend 3 are extensible. That is, you can add your own.
- We provide an implementation of Triggers and Actions for Silverlight
- We add Behaviors, the concept that really is the namesake for the whole feature area in Blend: Behaviors build on Triggers and Actions and allows to encapsulate interactivity patterns that are far richer than those supported by Triggers and Actions alone.
So, what exactly are Triggers, Actions and Behaviors?
Triggers & Actions
Let’s begin with Triggers and Actions. Think of the following sentence:
When I press the button, the door opens.
This sentence includes an action, the opening off the door, and a trigger that causes the action to happen, pressing the button.
And that really is all an Action is: An activity in the most general sense. We will have built-in Actions that do common things such as playing storyboards, setting properties, setting state and many more, but really, an Action can do anything someone decides to write. Your imagination is the limit.
Just like Actions, Blend 3 will supply built-in Triggers, for example for common events. And again, Triggers are extensible so the community can create new ones. Here are a few examples for possible Triggers: TimerTrigger fires when a timer expires. One of our developers, Jeff Kelly, wrote a trigger right before mix09 that fires when you draw a particular mouse gesture. You could have triggers that fire when a data base element changes. Or when the network connection on your machine goes down. Again, only your imagination is the limit.
There are many bits of interactivity that cannot easily be encapsulated with Triggers and Actions. For example, if you want to make something drag-able on a canvas, you need to deal with at least three events: You need to begin a drag when the mouse is pressed, update when the mouse is moved during a drag, and terminate the drag when the mouse is released. Also, you need to preserve state. And this is exactly what behaviors allow you to do. Behaviors let you encapsulate multiple related or dependent activities plus state in a single reusable unit.
We will explain more details in future posts.
Target & Source
As we talk about Triggers and Actions, there are a couple of other important pieces that play into this. Let’s repeat the sentence from above, slightly modified:
When I press the “open exit door” button, the exit door opens.
This is getting rather colorful. Let’s look at the magenta part of the sentence first. Imagine we have a room with multiple doors that can be opened from a control panel. “The door opens”, as in the original phrase, is not specific enough, we need to know which door the “open” action should be applied to. Many actions therefore have a Target property that points to the element that the action should be applied to.
As you apply Actions by dragging & dropping them on a UI element on the artboard, we make the object you are dropping the Action on the default target for your Action. We also give you a property editor in the property inspector that lets you choose any other object in the scene as your target.
The first part of the original sentence also is not specific enough: “When I press the button” does not really tell me which button. The revised sentence therefore has a clarification, in orange, that clearly states which button we mean. Many Triggers therefore have a Source property that allows you to configure on which UI element your trigger is supposed to listen.
Next in this series, we will discuss an example Action. Stay tuned…