为什么我的EventTrigger对我的左键不起作用?

时间:2019-10-16 09:12:33

标签: c# .net wpf xaml

因此,我正在尝试为我的样式设置EventTrigger,它似乎仅注册MouseOver事件,而不注册MouseDown事件。而且我不确定为什么。

<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>

1 个答案:

答案 0 :(得分:1)

您可以尝试使用PreviewMouseDown