DCSIMG
Blend Behaviors via Attached Properties – Part 2 - Essential WPF

Blend Behaviors via Attached Properties – Part 2

In my previous post I’ve talked about Blend 3 attached behaviors, and explained how to use them. In this post I would like to dive into more details, explaining the differences between Triggers, Actions and Behaviors types and how to create custom triggers.

Triggers and Actions

Actually blend 3 attached behavior comes in two flavors:

  • Triggers and Actions
  • Behaviors

You can think of triggers and actions as cause-and-effect relationships. Trigger represents the cause. For example we want to do something when routed-event is raised or property has changed. Action is the effect caused by, for example we want to navigate to the next page when routed-event is raised, so “navigate to next page” is the action and “routed-event raised” is the cause of that action.

Behaviors

While action is invoked only by a trigger, behavior has its own invocation logic and it’s not depended on any trigger.

Custom Action

Using Blend 3 SDK we can easily extend each element described so far. In this post I’ll concentrate on how to create a custom action.

Lets say that we want to create a custom action that displays a simple message box when invoked, with specific parameters.

So first thing we have to do is to install Blend 3 or Blend 3 SDK.

Now we should do as follows:

  1. Add reference to System.Windows.Interactivity.
  2. Create new class called ShowMessageBoxAction derived from TriggerAction<DependencyObject>. TriggerAction<T> represents a trigger base class where T is the associated type.
  3. Add dependency properties for each one of the required message box parameters.
  4. Override the Invoke method to show the message box.

    public class ShowMessageBoxAction : TriggerAction<DependencyObject>

    {

        #region Overrides

 

        protected override void Invoke(object parameter)

        {

            MessageBox.Show(Message, Caption, Buttons);

        }

 

        #endregion

 

        #region Dependency Properties

 

        public string Message { ... }

        public string Caption { ... }

        public MessageBoxButton Buttons { ... }

 

        #endregion

    }

Note that you can place all actions in a different assembly, and add a reference to it.

Now looking at Blend 3, we should see our custom action.

image

To use the ShowMessageBoxAction all we have to do is to create a simple button for example, drag-drop this action on the button we’ve just created and set the action’s trigger (cause) and properties.

image

You may download the source code from here.

 

If you enjoyed reading this post feel free to keep tracking. In my next post I’ll explain how to create custom triggers.

Published Monday, November 09, 2009 10:18 PM by Tomer Shamam

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems