Message Triggers

Explanation

A message trigger is something that causes a routed message to be sent. There are three types of message triggers provided out of the box: EventMessageTrigger, GestureMessageTrigger, and AttachedEventMessageTrigger. They can all be found in the namespace Caliburn.PresentationFramework.Triggers. This mechanism is entirely extensible. To create your own trigger types, simply inherit from BaseMessageTrigger.

Details (Samples - MessageTriggers)

EventMessageTrigger

This type of trigger sends messages when standard .NET events are raised. To declare the event, use the EventName property.

Long Version - Silverlight
<Button Content="Divide (Trigger Collection w/ Explicit Parameters)">
    <cm:Message.Triggers>
        <ct:RoutedMessageTriggerCollection>
            <ct:EventMessageTrigger EventName="Click">
                <ct:EventMessageTrigger.Message>
                    <ca:ActionMessage MethodName="Divide"
                                      OutcomePath="DivideResult.Text">
                        <cm:Parameter ElementName="left"
                                      Path="Text" />
                        <cm:Parameter ElementName="right"
                                      Path="Text" />
                    </ca:ActionMessage>
                </ct:EventMessageTrigger.Message>
            </ct:EventMessageTrigger>
        </ct:RoutedMessageTriggerCollection>
    </cm:Message.Triggers>
</Button>

Long Version - WPF
<Button Content="Divide (Trigger Collection w/ Explicit Parameters)">
    <cal:Message.Triggers>
        <cal:RoutedMessageTriggerCollection>
            <cal:EventMessageTrigger EventName="Click">
                <cal:EventMessageTrigger.Message>
                    <cal:ActionMessage MethodName="Divide"
                                   OutcomePath="DivideResult.Text">
                        <cal:Parameter Value="{Binding ElementName=left, Path=Text}"/>
                        <cal:Parameter Value="{Binding ElementName=right, Path=Text}"/>
                    </cal:ActionMessage>
                </cal:EventMessageTrigger.Message>
            </cal:EventMessageTrigger>
        </cal:RoutedMessageTriggerCollection>
    </cal:Message.Triggers>
</Button>

Note: The only difference between WPF and Silverlight is in the parameter declaration and the namespaces.

ShortVersion
<Button Content="Divide (Attachment w/ Explicit Parameters)"
        cal:Message.Attach="[Event Click] = [Action Divide(left.Text, right.Text) : DivideResult.Text]" />

GestureMessageTrigger

This type of trigger sends messages based on WPF's gesture mechanism. Use the MouseAction, Modifiers and Key properties. A limited version is available for Silverlight.

Long Version - Silverlight
<Ellipse Width="20"
         Height="20"
         Fill="Yellow"
         ToolTipService.ToolTip="Divide (Trigger Collection Gesture: Control + Left Click)">
    <cm:Message.Triggers>
        <ct:RoutedMessageTriggerCollection>
            <ct:GestureMessageTrigger MouseAction="LeftClick"
                                      Modifiers="Control">
                <ct:GestureMessageTrigger.Message>
                    <ca:ActionMessage MethodName="Divide"
                                      OutcomePath="DivideResult.Text">
                        <cm:Parameter ElementName="left"
                                      Path="Text" />
                        <cm:Parameter ElementName="right"
                                      Path="Text" />
                    </ca:ActionMessage>
                </ct:GestureMessageTrigger.Message>
            </ct:GestureMessageTrigger>
        </ct:RoutedMessageTriggerCollection>
    </cm:Message.Triggers>
</Ellipse>

Long Version - WPF
<Ellipse Width="20"
         Height="20"
         Fill="Yellow"
         ToolTip="Divide (Trigger Collection Gesture: Control + Left Click)">
    <cal:Message.Triggers>
        <cal:RoutedMessageTriggerCollection>
            <cal:GestureMessageTrigger MouseAction="LeftClick"
                                   Modifiers="Control">
                <cal:GestureMessageTrigger.Message>
                    <cal:ActionMessage MethodName="Divide"
                                   OutcomePath="DivideResult.Text">
                        <cal:Parameter Value="{Binding ElementName=left, Path=Text}" />
                        <cal:Parameter Value="{Binding ElementName=right, Path=Text}" />
                    </cal:ActionMessage>
                </cal:GestureMessageTrigger.Message>
            </cal:GestureMessageTrigger>
        </cal:RoutedMessageTriggerCollection>
    </cal:Message.Triggers>
</Ellipse>

Note: The only difference between WPF and Silverlight is in the parameter declaration and the namespaces.

Short Version
<Ellipse Width="20"
         Height="20"
         Fill="Red"
         ToolTipService.ToolTip="Divide (Attachment Gesture: Control + Left Click)"
         cal:Message.Attach="[Gesture MouseAction: LeftClick, Modifiers: Control] = [Action Divide(left.Text, right.Text) : DivideResult.Text]" />

AttachedEventMessageTrigger (WPF Only)

This type of trigger allows attached events to send messages. Declare the attached event using the RoutedEvent property.

Long Version (Only version)
<StackPanel>
    <cal:Message.Triggers>
        <cal:RoutedMessageTriggerCollection>
            <cal:AttachedEventMessageTrigger RoutedEvent="Button.Click">
                <cal:AttachedEventMessageTrigger.Message>
                    <cal:ActionMessage MethodName="Divide"
                                  OutcomePath="DivideResult.Text">
                        <cal:Parameter Value="{Binding ElementName=left, Path=Text}" />
                        <cal:Parameter Value="{Binding ElementName=right, Path=Text}" />
                    </cal:ActionMessage>
                </cal:AttachedEventMessageTrigger.Message>
            </cal:AttachedEventMessageTrigger>
        </cal:RoutedMessageTriggerCollection>
    </cal:Message.Triggers>
 
    <Button Content="Divide (Stack)" />
    <Button Content="Divide (Stack)" />
</StackPanel>

Short Version
<StackPanel cal:Message.Attach="[AttachedEvent Button.Click] = [Action Divide(left.Text, right.Text) : DivideResult.Text]">
    <Button Content="Divide (Stack)" />
    <Button Content="Divide (Stack)" />
</StackPanel>

Last edited Oct 23, 2009 at 7:53 PM by EisenbergEffect, version 9