WPF ColorAnimation BlinkAffect

时间:2018-01-11 23:00:42

标签: c# wpf coloranimation

我正在使用DataTemplate,并且Background绑定到用户定义的Brush。

作为一个例子

 <DataTemplate x:Key="ColumnText">
        <TextBlock x:Name="TextCell"
                      Background="{Binding Column.CustBackground}" // DependencyProperty
                      IsReadOnly="True" />
        <DataTemplate.Triggers>
           <DataTrigger Binding="{Binding **SomeProperty**}" Value="True">
           //Animation Code Here
           //.....
            </DataTrigger>
        </DataTemplate.Triggers>
  1. 我希望有一个眨眼效果,在我的情况下可以定义为 - &gt; &#34;快速改变两种颜色之间的背景(当前到新的或任何两种颜色)&#34;以一定的速度进行一定次数(比方说5次)。
  2. 我们对眨眼效果进行了罚款。我想恢复到以前的背景。
  3. 每当某些财产发生变化时,我们都会重复步骤1&amp; 2.
  4. 如果有人可以指导我如何实现这一点,那就太棒了。

    谢谢。

1 个答案:

答案 0 :(得分:0)

简单路径上的5倍闪烁示例:mousedown:

的.xaml

<Window x:Class="WpfApplication13.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:WpfApplication13"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Path Fill="Green" MouseDown="eventMarkerPath_MouseDown" Name="eventMarkerPath" 
  Data="M 5,15 L5,10 L10,0 L 0,0 L 5,10" >
        <Path.Resources>
            <Storyboard  x:Key="storyboard">
                <ColorAnimationUsingKeyFrames RepeatBehavior="5x" Duration="0:0:0.3" AutoReverse="True"  Storyboard.TargetName="eventMarkerPath" Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)">
                    <EasingColorKeyFrame KeyTime="0" Value="Green" ></EasingColorKeyFrame>
                    <EasingColorKeyFrame KeyTime="0:0:0.150" Value="Blue"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </Path.Resources>

        <i:Interaction.Triggers>
            <i:EventTrigger SourceObject="{Binding}" EventName="AnimationTriggerEvent">
                <i:EventTrigger.Actions>
                    <ei:ControlStoryboardAction Storyboard="{StaticResource storyboard}" ControlStoryboardOption="Play"/>
                </i:EventTrigger.Actions>
            </i:EventTrigger>
        </i:Interaction.Triggers>
      </Path>
</Grid>
</Window>

的.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var animationItem = new MyItem();
        this.DataContext = animationItem;
    }



    private void eventMarkerPath_MouseDown(object sender, MouseButtonEventArgs e)
    {
        ((sender as FrameworkElement).DataContext as MyItem).CauseAnimation();
    }
}

public class MyItem
{
    public void CauseAnimation()
    {
        if (this.AnimationTriggerEvent != null)
            this.AnimationTriggerEvent(this, null);
    }

    public event EventHandler AnimationTriggerEvent;
}