Making it Easier to Write Behaviors
July 9th, 2009 | 9 Comments
Behaviors make it easier for designers to be able to do cool things that would otherwise require writing code. Of course, a behavior doesn’t just magically appear out of thin air.* It requires someone actually writing it.
While one our goals was to make it easy for someone to write a Behavior, there is a certain amount of boilerplate code that you need to be aware of before you get to writing code that is more familiar to you. To help make that process easier, we are going to be providing Item Templates that can be accessed from both Expression Blend 3 as well as Visual Studio 2008.
Here are the Action, Behavior, and Trigger item templates in Blend 3:

…and here they are in Visual Studio:
These item templates contain the boilerplate code I referred to earlier, so with a few clicks, you are up and running with something that Blend recognizes as a Behavior, Action, or Trigger
For example, here is what a Trigger looks like when created via our Item Template:
- public class Trigger1 : TriggerBase<DependencyObject>
- {
- protected override void OnAttached()
- {
- base.OnAttached();
- // Insert code that you want to run when the Trigger is attached to an object.
- }
- protected override void OnDetaching()
- {
- base.OnDetaching();
- // Insert code that you would want run when the Trigger is removed from an object.
- }
- //
- // To invoke any associated Actions when this Trigger gets called, use
- // this.InvokeActions(o) where o is an object that you can pass in as a parameter
- //
- }
We create all of the infrastructure code that makes your Trigger a trigger, and we also provide you with some helpful comments on what to do. Because our behaviors functionality requires referencing a DLL that we provide, that referencing step is taken care of automatically for you as well. Wouldn’t it be great if everything in life were as simple as this? Ok, I’ll stop now!
How this is Possible
Everything I’ve written above uses the Item Templates format documented on MSDN. What is new is how these templates get installed. We will be releasing in the near future a standalone Expression Blend SDK that makes it possible for you to create and redistribute the various new components we introduced as a part of Blend 3. You can read more about this on a blog post I wrote for the Expression Blend & Design team blog.
Cheers!
Kirupa
*That feature is currently being prototyped in an underground bunker in an undisclosed location.





July 10th, 2009 at 6:46 am
I want to know how to use SQL server 2005/08 with WPF. can you cover that if your next blog posts. It’ll help a lot.
July 10th, 2009 at 10:06 am
[...] improvements (for example, the direct selection improvements) … but what about behaviors (1, 2, 3)? Whoa! Can we say [...]
July 20th, 2009 at 10:34 pm
[...] Behavior Templates for Blend and Visual Studio – Kirupa shares some ItemTemplates to help when creating new Actions, Behaviors and Triggers [...]
July 29th, 2009 at 2:35 am
Great thing – but it doesn’t the item templates do not show up in localized versions of VS2008.
I wrote a short description how to fix this
http://manni-at.spaces.live.com/blog/cns!82CF2A6C0D0C6F3C!203.entry
July 30th, 2009 at 8:45 am
[...] Behavior Templates for Blend and Visual Studio – Kirupa shares some ItemTemplates to help when creating new Actions, Behaviors and Triggers [...]
July 30th, 2009 at 8:52 am
[...] Behavior Templates for Blend and Visual Studio – Kirupa shares some ItemTemplates to help when creating new Actions, Behaviors and Triggers [...]
August 3rd, 2009 at 4:44 pm
[...] Behavior Templates for Blend and Visual Studio – Kirupa shares some ItemTemplates to help when creating new Actions, Behaviors and Triggers [...]
September 19th, 2009 at 5:00 pm
[...] Behavior Templates for Blend and Visual Studio – Kirupa shares some ItemTemplates to help when creating new Actions, Behaviors and Triggers [...]
October 1st, 2009 at 3:26 pm
The VentureIntoGaming_ExpressionBlend3_StarterKit is password protected, any ideas?