我有一个简单的彩色动画:
<TextBlock Foreground="{DynamicResource xxx}" Text="{Binding DataContext.Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type xcdg:DataCell}}}" >
<TextBlock.Style>
<Style TargetType="TextBlock" >
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.ValueChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type xcdg:DataCell}}}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard >
<ColorAnimation AutoReverse="True" From="{StaticResource NormalForeground}" To="{StaticResource ModifiedForeground}" Duration="0:0:1" Storyboard.TargetProperty="Foreground.Color" FillBehavior="Stop" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
如您所见,当 DataContext.ValueChanged 为真时会发生这种情况。这是一个布尔值,每次需要时都会变为 true -> false。通常动画需要 1 秒来执行,我想要的是在动画已经运行时发生多个 DataContext.ValueChanged 时延长时间。
所以如果我这样做:
DataContext.ValueChanged: start animation for 1 second.
After 0.5 seconds: DataContext.ValueChanged: the animation will last 1.5 second.
After 1.0 seconds: DataContext.ValueChanged: the animation will last 1.5 second more.
基本上,每次 DataContext.ValueChanged 值变为 true 时,都会向 ColorAnimation 添加一秒,并且它必须保留 To: 颜色,直到它必须开始面朝外。
谢谢!
编辑:
我有这样的viewmodel代码:
(...)
if (condition)
{
this.ValueChanged = true;
this.ValueChanged = false;
}
(...)
此代码每秒调用一次,并且动画持续时间也设置为一秒,因此如果条件满足预期的输出,则 TextBox 将保留 To 颜色,直到条件为假,然后执行淡出到 From 颜色。
希望现在更清楚!
编辑2:
这是一个演示问题的项目。执行它并在 TextBox 上键入,如果每个键的键入速度超过一秒,则文本应保持红色。当您减少到每个键不到一秒时,动画应该完全执行:
编辑 3:
在此处添加项目的源代码:
XAML:
<Window x:Class="AnimationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Background="#444444">
<TextBox Name="Box" Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="White" Background="#444444" >
</TextBox>
<TextBlock Text="{Binding Text}" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" >
<TextBlock.Style>
<Style TargetType="TextBlock" >
<Style.Triggers>
<DataTrigger Binding="{Binding ValueChanged}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard >
<ColorAnimation AutoReverse="True" From="White" To="Red" Duration="0:0:1" Storyboard.TargetProperty="Foreground.Color" FillBehavior="Stop" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</Window>
代码:
using System.ComponentModel;
using System.Windows;
namespace AnimationTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window , INotifyPropertyChanged
{
private string _text;
private bool _valueChanged;
public string Text
{
get { return _text; }
set
{
_text = value;
this.OnPropertyChanged("Text");
this.ValueChanged = true;
this.ValueChanged = false;
}
}
public bool ValueChanged
{
get
{
return _valueChanged;
}
set
{
_valueChanged = value;
this.OnPropertyChanged("ValueChanged");
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}