c# - XAML 中的动画按钮背景颜色

标签 c# wpf xaml resourcedictionary wpf-animation

我是 WPF 和 XAML 的新手,所以我有 ResourceDictionary(目前只有一个按钮):

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

    <Style x:Key="ButtonProduct" TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="Border"  
                            CornerRadius="0" 
                            BorderThickness="0"
                            Focusable="False"
                            BorderBrush="Transparent" Background="White">
                        <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter  Property="Background" Value="#52b0ca"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

鼠标悬停时按钮的颜色会发生变化,但我怎样才能改变淡入和淡出,以实现颜色的平滑过渡?

最佳答案

您可以使用 EventTriggerMouseEnterMouseLeave 上启动 ColorAnimation:

<ControlTemplate TargetType="{x:Type Button}">
   <Border Name="Border" CornerRadius="0" BorderThickness="0" Focusable="False" BorderBrush="Transparent" Background="White">
      <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
   </Border>
   <ControlTemplate.Triggers>
      <EventTrigger RoutedEvent="MouseEnter">
         <BeginStoryboard>
            <Storyboard>
               <ColorAnimation From="White" To="#52b0ca" Duration="0:0:1" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
            </Storyboard>
         </BeginStoryboard>
      </EventTrigger>
      <EventTrigger RoutedEvent="MouseLeave">
         <BeginStoryboard>
            <Storyboard>
               <ColorAnimation From="#52b0ca" To="White" Duration="0:0:1" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
            </Storyboard>
         </BeginStoryboard>
      </EventTrigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

关于c# - XAML 中的动画按钮背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21969016/

相关文章:

c# - WPF 有条件地启用键绑定(bind)

c# - 软键盘与文本框重叠并使它们无法访问

c# - 如何在多边形内旋转位图?

c# - Entity Framework 通过在没有选择的情况下将当前值增加一来更新一列

javascript - 如何使用我的数据库条目创建下拉列表?

c# - 将具体实例注入(inject) Ninject 解析器

c# - 从 DynamicResource 为 BorderBrush 设置动画会使用该画笔为所有内容设置动画

c# - 从 Windows 窗体过渡到 WPF

C#-Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context.

c# - 是否可以运行托管在 Windows 服务中的 Orleans