2017-01-02 205 views
0

我试图在按钮的背景创建一个带有颜色变化效果的按钮。但不工作,请你帮忙。C#WPF自定义闪烁按钮

这是我的代码。

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

您尝试动画化的SolidColorBrush的颜色属性,但您已设置的按钮,将一个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> 

它的动画渐变停止的色彩属性。

+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> 

+0

真的非常感谢,但我已经在#2上使用解决方案回复。 –