Blend 3 has added two new assemblies which consists of new functionalities including advance behaviors
- Microsoft.Expression.Interactions.dll
- System.Windows.Interactivity.dll
These advance behavior classes lets you create attached behaviors with simplicity. Code snippet below shows how to create a behavior which can be applied on any UIElement’s Routed event and when that event is triggered the given command is invoked with its command parameters. These behaviors help us write clean code and we dont have to deal with click events when using M-V-vM pattern. We can wite our command and bind that command to any routed event on any user interface element and implement the command on view model.
Code Snippet
- using System;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Interactivity;
- using EventTrigger = System.Windows.Interactivity.EventTrigger;
- namespace Behaviors
- {
- /// <summary>
- /// Behaviour helps to bind any RoutedEvent of UIElement to Command.
- /// </summary>
- [DefaultTrigger(typeof(UIElement), typeof(EventTrigger), "MouseLeftButtonDown")]
- public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
- {
- /// <summary>
- /// Dependency property represents the Command of the behaviour.
- /// </summary>
- public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
- “CommandParameter”,
- typeof(object),
- typeof(ExecuteCommandAction),
- new FrameworkPropertyMetadata(null));
- /// <summary>
- /// Dependency property represents the Command parameter of the behaviour.
- /// </summary>
- public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
- “Command”,
- typeof(ICommand),
- typeof(ExecuteCommandAction),
- new FrameworkPropertyMetadata(null));
- /// <summary>
- /// Gets or sets the Commmand.
- /// </summary>
- public ICommand Command
- {
- get
- {
- return (ICommand)this.GetValue(CommandProperty);
- }
- set
- {
- this.SetValue(CommandProperty, value);
- }
- }
- /// <summary>
- /// Gets or sets the CommandParameter.
- /// </summary>
- public object CommandParameter
- {
- get
- {
- return this.GetValue(CommandParameterProperty);
- }
- set
- {
- this.SetValue(CommandParameterProperty, value);
- }
- }
- /// <summary>
- /// Invoke method is called when the given routed event is fired.
- /// </summary>
- /// <param name=”parameter”>
- /// Parameter is the sender of the event.
- /// </param>
- protected override void Invoke(object parameter)
- {
- if (this.Command != null)
- {
- if (this.Command.CanExecute(this.CommandParameter))
- {
- this.Command.Execute(this.CommandParameter);
- }
- }
- }
- }
- \
Once you create a class with the above code and open the project in blend and open any UI, you can see the following view in the asset tab. Marked in red is our behavior.

Drop the behavior onto any UIElement

Finally you can see how we can select the Event and bind the command and command parameters in the properties tab.

XAML for above blend designer setting.
Code Snippet
- <Grid>
- <i:Interaction.Triggers>
- <i:EventTrigger EventName=”MouseLeftButtonDown”>
- <Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
- </i:EventTrigger>
- </i:Interaction.Triggers>