WPF,在样式中创建笔刷动画.按钮样式的触发器



我现在正在开发Button样式,我已经包含了一个控件模板和样式触发器。现在我想让背景刷褪色到某个颜色时,鼠标进入按钮。但是彩色动画无法使用笔刷。从昨天起我就一直坚持着。以下是我迄今为止在按钮样式中所做的操作:

<Converters:MathConverter x:Key="MathConverter" />
<Converters:ColorToBrushConverter x:Key="ColorToBrushConverter" />
<Style TargetType="Button">
    <Setter Property="FontSize"
            Value="{Binding FontSizeButton}" />
    <Setter Property="Margin"
            Value="{Binding FontSizeBase, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE/3}" />
    <Setter Property="Padding"
            Value="{Binding FontSizeButton, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE/3}" />
    <Setter Property="MinWidth"
            Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" />
    <Setter Property="Width"
            Value="NaN" />
    <Setter Property="Background"
            Value="{Binding BrushBackButton}" />
    <Setter Property="BorderBrush"
            Value="{Binding BrushBorder}" />
    <Setter Property="BorderThickness"
            Value="{Binding BorderThicknessBase}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="MainBorder" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="Content"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      TextElement.Foreground="{TemplateBinding Foreground}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <!--
        <Trigger Property="IsMouseOver"
                 Value="True">
            <Setter Property="Background"
                    Value="{Binding BrushBackButtonOver}" />
        </Trigger>
        -->
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard TargetProperty="Background">
                    <ColorAnimation To="Blue" Duration="10"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

对于这类问题,您应该使用VisualStateManager。

它可能会让你找到这样的东西:

   <Style TargetType="{x:Type Button}">
     <Setter Property="Template">
       <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
           <Border x:Name ="Border" Background="LightBlue">
             <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                   <VisualStateGroup.Transitions>
                     <VisualTransition GeneratedDuration="0:0:0.2"/>
                   </VisualStateGroup.Transitions>
                   <VisualState x:Name="Normal"/>
                   <VisualState x:Name="Pressed">
                     <Storyboard>
                       <ColorAnimation 
                    Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                    To="Red"/>
                     </Storyboard>
                   </VisualState>
                   <VisualState x:Name="MouseOver">
                      <Storyboard>
                         <ColorAnimation
                   Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                   To="Blue"/>
                      </Storyboard>
                   </VisualState>
                </VisualStateGroup>
             </VisualStateManager.VisualStateGroups>
             <ContentPresenter Content="{TemplateBinding Content}"/>
         </Border>
[...]

[编辑]关于您的特定问题,您应该将触发器放在ControlTemplate的触发器中。

 <ControlTemplate TargetType="{x:Type Button}">
 [...]
  <ControlTemplate.Triggers>
    <EventTrigger RoutedEvent="MouseEnter">
        <BeginStoryboard>
           <Storyboard  TargetName="Border" TargetProperty="(Background).(SolidColorBrush.Color)">
               <ColorAnimation To="Blue" Duration="0:0:0.2"/>
           </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
   </ControlTemplate.Triggers>

相关内容

  • 没有找到相关文章

最新更新