-1

So I'm trying to setup a EventTrigger for my style and it seems to only register the MouseOver event but not not the MouseDownone. And I'm not sure why.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type Button}" x:Key="KiwiButton">
        <!--<Setter Property="Background" Value="#2ecc71"/>-->
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontFamily" Value="Fonts/#Roboto"/>


        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center">


                        </ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>



        <Style.Triggers>
            <EventTrigger RoutedEvent="MouseEnter">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Duration="0:0:0.2"
                                            Storyboard.TargetProperty="Background.Color"
                                            To="#27ae60" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>


            <EventTrigger RoutedEvent="MouseLeave">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Duration="0:0:0.2"
                                            Storyboard.TargetProperty="Background.Color"
                                            To="#2ecc71" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>


            <EventTrigger RoutedEvent="MouseDown">  
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Duration="0:0:0.2"
                                            Storyboard.TargetProperty="Background.Color"
                                            To="Orange" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>


            <EventTrigger RoutedEvent="MouseUp">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Duration="0:0:0.2"
                                            Storyboard.TargetProperty="Background.Color"
                                            To="#27ae60" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>


        </Style.Triggers>

    </Style>

</ResourceDictionary>
Filburt
  • 17,626
  • 12
  • 64
  • 115
Riley Varga
  • 670
  • 1
  • 5
  • 15
  • As noted in the marked duplicates, the `Button` control has components that already handle the `MouseDown` event, so it doesn't work its way back to the trigger. There are a number of alternatives, including just using the `PreviewMouseDown` event instead (also noted in the marked duplicates). – Peter Duniho Oct 16 '19 at 17:35

1 Answers1

1

You can try it with PreviewMouseDown

ascripter
  • 5,665
  • 12
  • 45
  • 68
Eric P.
  • 97
  • 1
  • 7
  • That worked! I wonder why though, I need to look up the difference between those two. – Riley Varga Oct 16 '19 at 09:17
  • Since you're interacting with a Framework (WPF), a lot of things happen behind the scènes without you knowing it. You'll find plenty of documentation online. Please Don't forget to mark your question as 'answered', and have a good day. – Eric P. Oct 16 '19 at 09:22
  • @Riley Varga All bubbling events (events without the prefix "Preview"), especially the input events, are marked as handled by the framework controls. Handled events stop propagating at the element that makes the event as handled. That's why other controls can't handle this event too, except you subscribe to an Routed Event by explicitly setting the `handledEventsToo` parameter of the [`AddHandler`](https://docs.microsoft.com/en-us/dotnet/api/system.windows.uielement.addhandler?view=netframework-4.8#System_Windows_UIElement_AddHandler_System_Windows_RoutedEvent_System_Delegate_System_Boolean_) – BionicCode Oct 16 '19 at 15:28
  • method to `true`. Since class handlers have higher precedence than instance handlers, class handlers eventually get invoked too in situations where the instance handlers marks an event as handled. – BionicCode Oct 16 '19 at 15:28