Search code examples
wpfxamlrouted-eventsevent-triggers

In WPF, is it possible to specify multiple routed events for a single event trigger?


I have an event trigger that I want to be fired in response to two different routed events. I don't want to repeat the event response code (in XAML) twice. Can I specify multiple routed events for a single event trigger declaration?

Example of a single event:

<Style.Triggers>
    <EventTrigger RoutedEvent="Button.MouseEnter">
        <--XAML MAGIC-->
        ...

Solution

  • Sorry... WPF's implementation of the EventTrigger only allows one routed event.

    Typically you would be firing a storyboard from a routed event. I would suggest the following compromise:

    <!--Define a storyboard as a resource-->
    <Storyboard x:Key="MyStoryboard1">
       <!--Many properties and etc...-->
    </Storyboard>
    
    <Style.Triggers>
       <EventTrigger RoutedEvent="Button.MouseEnter">
          <BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
             <!--Other properties/name if necessary-->
          </BeginStoryboard>
       </EventTrigger>
       <EventTrigger RoutedEvent="Button.MouseDown">
          <BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
             <!--Other properties/name if necessary-->
          </BeginStoryboard>
       </EventTrigger>
    </Style.Triggers>
    

    The concept is to reduce duplicate code by sharing resources.

    Hope this helps!