[英]How to dynamically choose TextBlock animation type in wpf?
I would like to create simple animation which type is based on some value.我想创建简单的 animation 类型基于某个值。 I need to change color of text in TextBlock control, but the target color is depending on bounded variable.
我需要更改 TextBlock 控件中文本的颜色,但目标颜色取决于有界变量。 I've already created 2 DataTriggers and depending on a value of my bounded variable, a proper animation should start.
我已经创建了 2 个 DataTriggers,并且根据我的有界变量的值,应该启动一个适当的 animation。 At the beginning everything seems to work properly (AnimationValue is equal to 0 on start), when the value changes to 1, animation runs, then value returns to 0. The problem is, when the value turns to 2 (Animation with another color also runs) and then again 0, the first animation is not going to run anymore but the second one still works in a proper way.
一开始一切似乎都正常(AnimationValue在开始时等于0),当值变为1时,animation运行,然后值返回0。问题是,当值变为2时(另一种颜色的动画也运行)然后再次为 0,第一个 animation 将不再运行,但第二个仍然以正确的方式工作。
<Border
Grid.Column="0"
Background="Transparent">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="32"
Foreground="White"
Text="MyText">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AnimationValue}" Value="1">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="Gray"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="White"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
<DataTrigger Binding="{Binding Path=AnimationValue}" Value="2">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="Firebrick"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="White"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
There is no problem with setting a correct value - i've checked it using a debugger and everytime correct value is set.设置正确的值没有问题 - 我已经使用调试器检查过它,并且每次都设置了正确的值。 0 is always between 1 and 2. DataContext is also not a problem - a connection between View and ViewModel is not being broken.
0 始终介于 1 和 2 之间。DataContext 也不是问题 - View 和 ViewModel 之间的连接没有中断。 I've noticed that broken animation is always the first one in xaml file.
我注意到损坏的 animation 始终是 xaml 文件中的第一个。 Now the "Gray" animation stops working correctly, but if i change order in xaml file, firebrick animation will be the broken one.
现在“灰色”animation 停止正常工作,但如果我更改 xaml 文件中的顺序,耐火砖 animation 将是损坏的。 Thanks for any help.
谢谢你的帮助。
It looks like running StoryBoard
s in triggers is a weird thing.看起来在触发器中运行
StoryBoard
是一件奇怪的事情。 I don't have a technical explanation as to how they behave but here is a SO question where I found an answer for you.我对它们的行为方式没有技术解释,但这是一个SO question ,我在其中为您找到了答案。
Here is some code based on the answer above that works:这是一些基于上述答案的代码:
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AnimationValue}" Value="0">
<DataTrigger.EnterActions>
<StopStoryboard BeginStoryboardName="Animation1" />
<StopStoryboard BeginStoryboardName="Animation2" />
<BeginStoryboard x:Name="Animation0">
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="White"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding Path=AnimationValue}" Value="1">
<DataTrigger.EnterActions>
<StopStoryboard BeginStoryboardName="Animation0" />
<StopStoryboard BeginStoryboardName="Animation2" />
<BeginStoryboard x:Name="Animation1">
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="Gray"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding Path=AnimationValue}" Value="2">
<DataTrigger.EnterActions>
<StopStoryboard BeginStoryboardName="Animation0" />
<StopStoryboard BeginStoryboardName="Animation1" />
<BeginStoryboard x:Name="Animation2">
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
To="Firebrick"
Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.