动画文本颜色

发布于 2024-10-15 17:34:06 字数 856 浏览 2 评论 0原文

我需要在两种颜色之间设置自定义控件的文本颜色动画,这些颜色是从自定义控件的两个 Brush 属性中读取的。我的资源如下所示:

<SolidColorBrush x:Key="TextBrush">{TemplateBinding Foreground}</SolidColorBrush>
<SolidColorBrush x:Key="AltTextBrush">{TemplateBinding ForegroundAlt}</SolidColorBrush>

现在,我正在尝试使用 ColorAnimation 进行动画处理:

<ColorAnimation Storyboard.TargetName="MyControlText" Storyboard.TargetProperty="Foreground" To="{StaticResource AltTextBrush}" Duration="00:00:00.3000000" />

ColorAnimation 似乎需要一个 Color 对象,而不是我试图传递的 Brush 。我想我可以编写一个 IValueConverter 来从画笔获取颜色,但在这样做之前,我想看看是否有更简单的方法来完成这项工作。以下是我的问题:

-- 是否有一种简单的方法可以在两个画笔资源之间设置动画,或者我是否需要提取动画的颜色?

-- 如果我需要提取颜色,IValueConverter 是最佳实践吗?

-- 最后,我是否走在正确的道路上,或者是否有更简单的解决方案来解决这个问题?

感谢您的帮助。

I need to animate the text color of a custom control between two colors, which are read from two Brush properties of the custom control. My resources look like this:

<SolidColorBrush x:Key="TextBrush">{TemplateBinding Foreground}</SolidColorBrush>
<SolidColorBrush x:Key="AltTextBrush">{TemplateBinding ForegroundAlt}</SolidColorBrush>

Right now, I am trying to animate using a ColorAnimation:

<ColorAnimation Storyboard.TargetName="MyControlText" Storyboard.TargetProperty="Foreground" To="{StaticResource AltTextBrush}" Duration="00:00:00.3000000" />

The ColorAnimation seems to want a Color object, rather than the Brush I am trying to pass. I think I can write an IValueConverter to get the color from the brush, but before I do that, I want to see if there is a simpler way to do the job. Here are my questions:

-- Is there a simple way to animate between two brush resources, or do I need to extract the color for animation?

-- If I need to extract the colors, is an IValueConverter best practice?

-- And finally, amI headed down the right road, or is there a simpler solution to this problem?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦冥 2024-10-22 17:34:06

尝试使用 Binding,它似乎像这样工作

To="{Binding Source={StaticResource TextBrush}, Path=Color}"

这是一个 xaml 示例

<Window.Resources>
    <SolidColorBrush x:Key="TextBrush">Black</SolidColorBrush>
    <Storyboard x:Key="blinkAnimation" Duration="0:0:5" >
        <ColorAnimation Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                        Storyboard.TargetName="TitleTextBlock"
                        To="{Binding Source={StaticResource TextBrush}, Path=Color}"
                        AutoReverse="True"
                        Duration="0:0:2"/>
    </Storyboard>
</Window.Resources>
<Grid Background="Black" Name="grid">
    <TextBlock x:Name="TitleTextBlock"
               Background="Black"
               Text="My Text"
               FontSize="32"
               HorizontalAlignment="Center"
               VerticalAlignment="Bottom"
               Foreground="White">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <StaticResource ResourceKey="blinkAnimation"/>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>
</Grid>

Tried with using a Binding and it seems to be working like this

To="{Binding Source={StaticResource TextBrush}, Path=Color}"

Here's a xaml example

<Window.Resources>
    <SolidColorBrush x:Key="TextBrush">Black</SolidColorBrush>
    <Storyboard x:Key="blinkAnimation" Duration="0:0:5" >
        <ColorAnimation Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                        Storyboard.TargetName="TitleTextBlock"
                        To="{Binding Source={StaticResource TextBrush}, Path=Color}"
                        AutoReverse="True"
                        Duration="0:0:2"/>
    </Storyboard>
</Window.Resources>
<Grid Background="Black" Name="grid">
    <TextBlock x:Name="TitleTextBlock"
               Background="Black"
               Text="My Text"
               FontSize="32"
               HorizontalAlignment="Center"
               VerticalAlignment="Bottom"
               Foreground="White">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <StaticResource ResourceKey="blinkAnimation"/>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文