【发布时间】:2019-10-16 09:12:33
【问题描述】:
所以我正在尝试为我的风格设置一个 EventTrigger,它似乎只注册了MouseOver 事件而不是MouseDownone。我不确定为什么。
<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>
【问题讨论】:
-
如标记的重复项中所述,
Button控件的组件已经处理了MouseDown事件,因此它无法返回触发器。有许多替代方案,包括仅使用PreviewMouseDown事件代替(也在标记的重复项中注明)。