WPF TextBlock ColorAnimation
I am totally new with WPF animations, and for the moment I face big problems with this.
I have a TextBlock
for which, as long as a property to my view model has a specific value, I want to run a ColorAnimation
to its background color. When the value to my property changes I want the background color for my TextBlock
to return back to the original one (probably Transparent). Here is what I discovered so far, but it still not working as I described.
<TextBlock Text="{Binding DatabaseTasks.Count, StringFormat= 'Count: {0}'}" VerticalAlignment="Center" Background="Transparent">
<TextBlock.Style>
<Style TargetType="TextBlock">
开发者_StackOverflow社区 <Style.Triggers>
<DataTrigger Binding="{Binding IsIdle}" Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
<ColorAnimation RepeatBehavior="Forever"
FillBehavior="Stop"
From="Red"
To="Transparent"
By="Blue"
Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Can someone fix this?
Thank you in advance!
EDIT: The binding works just fine and I tested it using a simple DataTrigger that just changes the background color. My problem is that animation does not stop and return to the orginal color. I am sure I miss something.
Adding the RemoveStoryboard
tag should do it.
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsIdle}" Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="tt" >
<Storyboard Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
<ColorAnimation RepeatBehavior="Forever"
FillBehavior="Stop"
From="Red"
To="Transparent"
By="Blue"
Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="tt"></RemoveStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
精彩评论