WPF文字颜色动画效果

文字颜色动态改变

实现该效果采用的方式是使用ColorAnimation类的Storyboard.TargetProperty动态改变 GradientStop的Color值。

实现效果

  • 实现代码
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>                        
                        <ColorAnimation Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever" To="Red" 
                                        Storyboard.TargetName="tb1"                                             Storyboard.TargetProperty="Foreground.GradientStops[1].Color"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Window.Triggers>
   
<Grid>
        <StackPanel >
            <TextBlock x:Name="tb1" Text="Test the Gradient Animation" FontSize="30" TextAlignment="Center">
                <TextBlock.Foreground>
                    <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"   >
                        <GradientStop Color="Yellow" Offset="0"/>
                        <GradientStop Color="Blue" Offset="1"/>
                    </LinearGradientBrush>
                </TextBlock.Foreground>
                <TextBlock.Effect>
                    <DropShadowEffect ShadowDepth="2" />
                </TextBlock.Effect>
            </TextBlock>
        </StackPanel>
    </Grid>

也可应用在图形对象上

posted @ 2021-07-24 20:52  moonsk  阅读(1055)  评论(0编辑  收藏  举报