为Button的内容定义样式

时间:2017-08-30 06:30:21

标签: wpf xaml datatrigger eventtrigger wpf-style

我的按钮Style中定义的以下通用ControlTemplate.Resources有什么问题?

<ControlTemplate x:Key="ZeroButtonTemplate" TargetType="Button">
  <ControlTemplate.Resources>
    <Style TargetType="{x:Type Rectangle}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsRunning}" Value="True">
          <Setter Property="Fill" Value="LightGray" />
        </DataTrigger>
        <EventTrigger RoutedEvent="Button.MouseEnter">
          <BeginStoryboard>
            <Storyboard>
              <ColorAnimation From="Black" To="Red" Duration="00:00:00.5" Storyboard.TargetProperty="Fill" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Style.Triggers>
    </Style>
  </ControlTemplate.Resources>

  <ContentPresenter Content="{TemplateBinding Content}" />
</ControlTemplate>

DataTriggerEventTrigger似乎都没有效果。

为了完成,我的Button定义如下:

<Button Template="{StaticResource ZeroButtonTemplate}">
  <Button.Content>
    <Rectangle Width="15" Height="15" Fill="Black" />
  </Button.Content>
</Button>

1 个答案:

答案 0 :(得分:1)

将动画的目标属性更改为Fill.Color

<ColorAnimation From="Black" To="Red" Duration="00:00:00.5" Storyboard.TargetProperty="Fill.Color" />