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