如何在WPF中进行文本颜色转换

时间:2016-04-02 23:05:25

标签: wpf

使用过渡属性在HTML中很容易。

<html>
    <head>
        <style> 
            p {background:black;color:yellow;transition:color 2s;}
            p:hover { color:red; }
        </style>
    </head>
    <body>
        <p>TEXT COLOR TRANSITION</p>
    </body>
</html>

在WPF中,它由以下内容完成。它需要一个停止作为填充行为,在鼠标离开时重新恢复为黄色:

<TextBlock Background="Black"
           Foreground="Yellow"
           Text="TEXT COLOR TRANSITION">
    <TextBlock.Triggers>
        <EventTrigger RoutedEvent="TextBlock.MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="0:0:2"
                                    Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                    To="Red" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="TextBlock.MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="0:0:2"
                                    FillBehavior="Stop"
                                    Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </TextBlock.Triggers>
</TextBlock>

0 个答案:

没有答案