如何在wpf中使文本块闪烁?

时间:2010-11-14 13:00:51

标签: wpf animation

我在WPF中创建了一个仪表板,其中包含一系列关键性能指标,每个指标都由三个值组成。

alt text

每当值改变时,我希望用户控件闪烁5秒钟。我想使控件的背景颜色切换文本块的前景色,文本块前景色更改为用户控件的背景颜色。

这整个WPF动画对我来说都是新手,所以任何帮助都会非常感激!

我的用户控件如下:

    

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="10" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="10" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock x:Name="TitleTextBlock" Text="Title" FontSize="32" HorizontalAlignment="Center" Grid.Row="0" FontFamily="OCR-A II" Foreground="White" VerticalAlignment="Bottom" />
    <TextBlock x:Name="Value1TextBlock" Text="0" FontSize="192" HorizontalAlignment="Center" Grid.Row="2" FontFamily="OCR-A II" VerticalAlignment="Center" Foreground="White" />
    <TextBlock x:Name="Value2TextBlock" Text="0" FontSize="32" HorizontalAlignment="Center" Grid.Row="4" FontFamily="OCR-A II" Foreground="White" VerticalAlignment="Top" />

</Grid>

1 个答案:

答案 0 :(得分:34)

要在文本更改时使TextBlock闪烁,您可以使用ColorAnimationUsingKeyFrames。文本绑定到名为TextTitle的属性。

<Window.Resources>
    <Storyboard x:Key="blinkAnimation" Duration="0:0:5" >
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
                                      Storyboard.TargetName="TitleTextBlock"
                                      AutoReverse="True">
            <ColorAnimationUsingKeyFrames.KeyFrames>
                <DiscreteColorKeyFrame KeyTime="0:0:0" Value="White"/>
                <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Black"/>
                <DiscreteColorKeyFrame KeyTime="0:0:2" Value="White"/>
                <DiscreteColorKeyFrame KeyTime="0:0:3" Value="Black"/>
                <DiscreteColorKeyFrame KeyTime="0:0:4" Value="White"/>
            </ColorAnimationUsingKeyFrames.KeyFrames>
        </ColorAnimationUsingKeyFrames>

        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                      Storyboard.TargetName="TitleTextBlock"
                                      AutoReverse="True">
            <ColorAnimationUsingKeyFrames.KeyFrames>
                <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Black"/>
                <DiscreteColorKeyFrame KeyTime="0:0:1" Value="White"/>
                <DiscreteColorKeyFrame KeyTime="0:0:2" Value="Black"/>
                <DiscreteColorKeyFrame KeyTime="0:0:3" Value="White"/>
                <DiscreteColorKeyFrame KeyTime="0:0:4" Value="Black"/>
            </ColorAnimationUsingKeyFrames.KeyFrames>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

<Grid Name="grid" Background="Black">
    <TextBlock x:Name="TitleTextBlock" Text="{Binding TextTitle, NotifyOnTargetUpdated=True}" FontSize="32" HorizontalAlignment="Center" Grid.Row="0" FontFamily="OCR-A II" Foreground="White" VerticalAlignment="Bottom" Background="Black">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="Binding.TargetUpdated">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <StaticResource ResourceKey="blinkAnimation"/>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </TextBlock.Triggers>    
    </TextBlock>
</Grid>

每次文本更改时,这将使TextBlock闪烁。请注意,在使用blinkAnimation之前必须在TextBlock上显式设置Background和Foreground,否则您将收到System.InvalidOperationException:'Background'属性不指向路径'(0)。(1)'中的DependencyObject。 / p>

<强>更新

要从后面的代码开始播放此动画,您可以执行此操作。

Storyboard blinkAnimation = TryFindResource("blinkAnimation") as Storyboard;
if (blinkAnimation != null)
{
    blinkAnimation.Begin();
}