Menu

Triggers and Actions model cause-and-effect relationships. A Trigger reacts to the cause and invokes one or more Actions.

Creating Triggers

A Trigger is an object that listens for a specific condition, such as an event firing or a property being set to a certain value, and invokes one or more associated Actions in response. Triggers range from an EventTrigger that fires an Action when a mouse or keyboard event is raised to a CollisionTrigger that fires an Action when two objects collide with each other. Triggers are extensible, so you can create custom triggers that are as creative as you want them to be. A Trigger must contain some basic syntax in order to be recognized by Behaviors at run time.

The following code sample shows a baseline Trigger:

C#�CopyCode imageCopy Code
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 want to 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.
    //
}

In the preceding code sample, the class is extended from TriggerBase, a type that is provided in the System.Windows.Interactivity.dll. Once the class is extended from the TriggerBase class, you can override the OnAttached and OnDetaching methods as shown in the code sample.

OnDetaching is called immediately before the Trigger is disassociated from its AssociatedObject. This means that the object will still be available from the scope of the Trigger, allowing your code to continue to act on it until OnDetaching is exited (for example, to unhook event handlers on it).

The most important thing to do with your Trigger is to actually have it fire. To have your Trigger fire, you must make a call to the InvokeActions method. When InvokeActions is called, any Actions associated with this Trigger will be notified to act.

Creating Actions

A Trigger is an object that can only listen for something to happen. An Action is an object that can only do something.

The following code sample shows a baseline Action:

C#�CopyCode imageCopy Code
public class MyAction : TriggerAction<DependencyObject>
{
    public MyAction()
    {
        // Insert code required for object creation below this point.
    }

    protected override void Invoke(object o)
    {
        // Insert code that defines what the Action will do when it is triggered or invoked.
    }
}

The Invoke method is what your Trigger will call, so make sure that any code that you want your Action to execute is accessible in some form within the Invoke method. The following code sample shows an Action that displays a MessageBox when invoked:

C#�CopyCode imageCopy Code
public class ShowMessageBoxAction : TriggerAction<DependencyObject>
{
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(ShowMessageBoxAction), new PropertyMetadata(""));

    public ShowMessageBoxAction()
    {
        // Insert code required for object creation below this point.
    }

    protected override void Invoke(object o)
    {
        MessageBox.Show(Message);
    }
}

In the preceding code sample, the Action is more complex than the simple Action shown previously, but the common characteristics that make something an Action still exist. ShowMessageBoxAction extends from TriggerAction, and overrides the Invoke method.

Targeting elements

The Action, by default, has access only to the AssociatedObject property, which is the element that it is attached to, although you can change what your Action is affecting. To have your Action target another element, extend the class representing your Action from TargetedTriggerAction instead of from TriggerAction:

C#�CopyCode imageCopy Code
public class MyAction : TargetedTriggerAction<DependencyObject>
{
    // See TriggerAction in the preceding code sample...
}

After you extend your class from TargetedTriggerAction, you can gain access to the targeted element by using the TargetName and Target properties. Your AssociatedObject will always point to the element that your Action is currently associated with, so avoid using AssociatedObject and the TargetName or Target properties interchangeably.

See Also


��Copyright � 2010 by Microsoft Corporation. All rights reserved.