C# 如果在完成动画之前发生多个触发器,如何增加动画的持续时间?
我有一个简单的彩色动画:C# 如果在完成动画之前发生多个触发器,如何增加动画的持续时间?,c#,wpf,animation,wpf-animation,coloranimation,C#,Wpf,Animation,Wpf Animation,Coloranimation,我有一个简单的彩色动画: <TextBlock Foreground="{DynamicResource xxx}" Text="{Binding DataContext.Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type xcdg:DataCell}}}" > <TextBlock.Style> <Style TargetType="Te
<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变为真时,都会向ColorAnimation添加一秒钟,并且它必须保留to:颜色,直到它必须开始面朝外
谢谢
编辑:
我在viewmodel代码中有如下内容:
(...)
if (condition)
{
this.ValueChanged = true;
this.ValueChanged = false;
}
(...)
此代码每秒调用一次,动画持续时间也设置为1秒,因此如果条件满足,则预期输出是文本框保留to颜色,直到条件为false,然后执行对From颜色的淡入淡出
希望现在更清楚了
编辑2:
下面是一个演示该问题的项目。执行它并在文本框中键入,如果每键键入速度超过1秒,则文本应保持红色。当减少到每关键帧少于1秒时,应完全执行动画:
编辑3:
在此处添加了项目的源代码:
XAML:
代码:
使用System.ComponentModel;
使用System.Windows;
名称空间动画测试
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
私有字符串_文本;
私有资产价值发生变化;
公共字符串文本
{
获取{return\u text;}
设置
{
_文本=值;
本条关于财产变更(“文本”);
this.ValueChanged=true;
this.ValueChanged=false;
}
}
公营房屋价值改变
{
得到
{
返回值已更改;
}
设置
{
_valueChanged=值;
本协议项下的不动产变更(“价值变更”);
}
}
公共主窗口()
{
初始化组件();
this.DataContext=this;
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
如果您提供一个代码示例来实际演示您的问题,那么您可能会得到更多的答案。@Sheridan:嗨,我添加了更多的代码来说明我在做什么和我期望什么。谢谢您的代码是否演示了您的问题?否。如果您将其粘贴到新项目中,您将看到。@Sheridan:添加了一个示例项目来演示该问题。谢谢。很有趣。。。而且很复杂。也许您需要两个ColorAnimation
s。。。一个将文本变为红色,另一个将文本变回白色?您可以像现在一样处理第一个事件(没有RepeatBehavior
),另一个可以从dispatchermer.Tick
事件处理程序从后面的代码启动,该事件处理程序设置为具有1秒的间隔
,并在文本
setter中启动。
(...)
if (condition)
{
this.ValueChanged = true;
this.ValueChanged = false;
}
(...)
<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));
}
}
}