Skip to content

EventTriggerBehavior

Ben Randall edited this page Sep 6, 2016 · 3 revisions

EventTriggerBehavior listens for a specific event on its source and executes an action when the event is fired.

This Behavior has two configurable properties:

This Behavior listens for the specified EventName to happen on SourceObject in order to execute.

  • EventName - The name of the event to listen for. This event must exist on the SourceObject
  • SourceObject - The object on which the behavior should listen for the event EventName on. If not provided, this will default to the element the behavior is attached to.

Sample Code

XAML

The following examples assume that the following XML Namespaces have been imported into your XAML document somewhere.
For example:

<Page 
    ...
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:Interactions="using:Microsoft.Xaml.Interactions.Core"
>

Attaching the behavior to an element.

<TextBox>
    <Interactivity:Interaction.Behaviors>
        <Interactions:EventTriggerBehavior EventName="KeyUp">
            <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource PaleYellowBrush}"/>
        </Interactions:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</TextBox>

Explicitly specifying the SourceObject of the behavior. Note that in this case it's being set to the containing element, but the source object can be set to any object.

<Button x:Name="button">
    <Interactivity:Interaction.Behaviors>
        <Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button}">
            <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource PaleYellowBrush}"/>
        </Interactions:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Button>