Pea*_*nut 8 wpf animation fade textblock
我在datatemplate中为数据网格中的单元格使用TextBlock.我有一个要求,说明当单元格的值发生变化时,文本应该:
目前,我使用TargetUpdated RoutedEvent触发动画,使文本逐渐消失然后再返回.但是,在文本已经改变屏幕上的值之后发生淡入淡出.
<DataTemplate>
<Border>
<TextBlock Name="templateTextBlock" Text="{Binding Path=FirstName, NotifyOnTargetUpdated=True}" />
</Border>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard AutoReverse="True">
<DoubleAnimation Storyboard.TargetName="templateTextBlock" Storyboard.TargetProperty="Opacity" To=".1" Duration="0:0:.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我的问题是如何实现所需的效果 - 淡出,更改文本,淡入?
非常感谢.
H.B*_*.B. 10
写了一个应该这样做的交互行为:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Run Code Online (Sandbox Code Playgroud)
<TextBlock Text="{Binding Name, NotifyOnTargetUpdated=True}">
<i:Interaction.Behaviors>
<b:AnimatedTextChangeBehavior AnimationDuration="0:0:0.1" />
</i:Interaction.Behaviors>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
class AnimatedTextChangeBehavior : Behavior<TextBlock>
{
public Duration AnimationDuration { get; set; }
private string OldValue = null;
private string NewValue = null;
DoubleAnimation AnimationOut;
DoubleAnimation AnimationIn;
protected override void OnAttached()
{
base.OnAttached();
AnimationOut = new DoubleAnimation(1, 0, AnimationDuration, FillBehavior.HoldEnd);
AnimationIn = new DoubleAnimation(0, 1, AnimationDuration, FillBehavior.HoldEnd);
AnimationOut.Completed += (sOut, eOut) =>
{
AssociatedObject.SetCurrentValue(TextBlock.TextProperty, NewValue);
OldValue = NewValue;
AssociatedObject.BeginAnimation(TextBlock.OpacityProperty, AnimationIn);
};
Binding.AddTargetUpdatedHandler(AssociatedObject, new EventHandler<DataTransferEventArgs>(Updated));
}
private void Updated(object sender, DataTransferEventArgs e)
{
string value = AssociatedObject.GetValue(TextBlock.TextProperty) as string;
AssociatedObject.BeginAnimation(TextBlock.OpacityProperty, AnimationOut);
NewValue = value;
if (OldValue == null)
{
OldValue = value;
}
AssociatedObject.SetCurrentValue(TextBlock.TextProperty, OldValue);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你不想为此使用Blend SDK的Interactivity,你可以将代码重构为一个单独的类并使用TextBlock的Loaded事件来进行设置.