我尝试在按钮的背景创建一个带有颜色变化效果的按钮。但是没有工作,请你帮忙。
这是我的代码。
XAML
<Button Margin="0,11,160,0" Name="btnShowNotification"
Click="btnOpenCashdrawer_Click" HorizontalAlignment="Right"
Style="{StaticResource NotificationOnButton}"
VerticalAlignment="Top" Grid.Column="1" />
的App.xaml
<Style x:Key="NotificationOnButton" TargetType="Button">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Panel.ZIndex" Value="50000"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="0" CornerRadius="5" Width="45" Height="30">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Gray" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Image Width="19" Source="Images/turn-notifications-on-button.png" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard BeginTime="00:00:00"
RepeatBehavior="Forever" AutoReverse="True"
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
<ColorAnimation From="Black" To="Red" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
答案 0 :(得分:0)
尝试下面的示例xaml以获取按钮闪烁动画。
的的Xaml 强>
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Name="button1" Content="Animate Button!" Width="150" Height="30" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="button1_Click" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard BeginTime="00:00:00"
RepeatBehavior="Forever"
Storyboard.TargetName="button1"
Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
<ColorAnimation From="Black" To="Red" Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
答案 1 :(得分:0)
您正在尝试为SolidColorBrush的Color属性设置动画,但您已将Button的背景设置为LinearGradientBrush,因此这不起作用。
试试这个:
<Style x:Key="NotificationOnButton" TargetType="Button">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Panel.ZIndex" Value="50000"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="0" CornerRadius="5" Width="45" Height="30">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop x:Name="sp" Color="Gray" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Image Width="19" Source="Images/turn-notifications-on-button.png" />
<Border.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard BeginTime="00:00:00" RepeatBehavior="Forever" AutoReverse="True"
Storyboard.TargetName="sp"
Storyboard.TargetProperty="Color">
<ColorAnimation From="Black" To="Red" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Border.Triggers>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
它动画化GradientStop的Color属性。