WPF 颜色动画 BlinkAffect

WPF ColorAnimation BlinkAffect

我正在使用 DataTemplate 并且背景绑定到用户定义的画笔。

举个例子

 <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. 我想要一个闪烁效果,在我的例子中可以定义为 -> "Rapidly changing Background between two colors(current to new or any two)" 以特定速度特定次数(比如 5 次)。
  2. 在我们改进了闪烁效果之后。我想恢复到以前的背景。
  3. 每次 属性 更改时,我们都会重复步骤 1 和 2。

如果有人可以指导我如何实现这一目标的指针/pusedo 代码,那就太好了。

谢谢。

一个简单的鼠标按下路径上的 5 倍闪烁示例:

.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;
}