2013-08-02 48 views
0

我想在DataTrigger触发时有一个闪烁/动画的按钮。我想动画按钮的背景。以下是我的xaml代码。Datatrigger上的按钮闪烁

<Window.Resources> 
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="True"> 
       <DataTrigger.EnterActions> 
        <BeginStoryboard Name="StartBlinking"> 
         <Storyboard> 
          <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </DataTrigger.EnterActions> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="False"> 
       <DataTrigger.EnterActions> 
        <RemoveStoryboard BeginStoryboardName="StartBlinking"/> 
       </DataTrigger.EnterActions> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

</Window.Resources> 
<Grid> 
    <Grid> 
     <Button x:Name="Button" Content="Button" Width="25" Height="25" Margin="158,62,320,224" Click="Button_Click"></Button> 
     <Button Style="{StaticResource ButtonStyle}" Content="Button" Focusable="False" Height="75" HorizontalAlignment="Left" Margin="23,146,0,0" Name="btnImgBrush" VerticalAlignment="Top" Width="160"></Button> 

    </Grid> 
</Grid> 

这里是后端代码:

public Boolean Notification 
    { 
     get { return new_notification; } 

     set 
     { 
      new_notification = value; 
      RaisePropertyChanged("Notification"); 
     } 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     if (Notification) 
     { 
      Notification = false; 
     } 
     else 
     { 
      Notification = true; 
     } 
    } 

但是,它没有工作。任何想法,为什么它不工作?

任何帮助,非常感谢,谢谢。

+0

你不能动画背景属性一样this..animation对象不能用于动画属性“背景”,因为它是不兼容的类型“System.Windows.Media.Brush”。 – Vishal

+0

你在哪里宣布你的通知财产..?在viewmodel或代码后面.. ?? – Vishal

+0

是的,我已经在viewmodel中声明了通知属性。有没有其他的方法来基于通知属性来设置按钮背景的动画效果? –

回答

4

最后的工作。谢谢:)

<Window.Resources> 
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Background"> 
      <Setter.Value> 
       <SolidColorBrush Color="Transparent"/> 
      </Setter.Value> 
     </Setter> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="True"> 
      <DataTrigger.EnterActions> 
       <BeginStoryboard Name="StartBlinking"> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </DataTrigger.EnterActions> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="False"> 
      <DataTrigger.EnterActions> 
       <RemoveStoryboard BeginStoryboardName="StartBlinking"/> 
      </DataTrigger.EnterActions> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

0

尝试这个 -

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> 
<Style.Triggers> 
         <DataTrigger Binding="{Binding Path=Notification,RelativeSource={RelativeSource AncestorType=Window}}" Value="True"> 
          <DataTrigger.EnterActions> 
           <BeginStoryboard Name="StartBlinking"> 
            <Storyboard> 
             <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="Transparent" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/> 
            </Storyboard> 
           </BeginStoryboard> 
          </DataTrigger.EnterActions> 
         </DataTrigger> 
         <DataTrigger Binding="{Binding Path=Notification,,RelativeSource={RelativeSource AncestorType=Window}}" Value="False"> 
          <DataTrigger.EnterActions> 
           <RemoveStoryboard BeginStoryboardName="StartBlinking"/> 
          </DataTrigger.EnterActions> 
         </DataTrigger> 
        </Style.Triggers> 
</Style> 
+0

我已经尝试过了。它不适用。 –

+0

你可以显示你的viewmodel代码,你已经定义了通知属性..?和一些更多的xaml代码 – Vishal

+0

我编辑了这个问题,并添加了其他xaml代码和viewmodel代码,我已经定义了通知属性。 –