Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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-->
        ...
like image 970
Jordan Parmer Avatar asked Sep 13 '25 06:09

Jordan Parmer


1 Answers

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!

like image 123
Josh G Avatar answered Sep 15 '25 14:09

Josh G