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>

也可应用在图形对象上


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM