gpt4 book ai didi

wpf - 动画文本颜色

转载 作者:行者123 更新时间:2023-12-04 19:46:22 24 4
gpt4 key购买 nike

我需要在两种颜色之间为自定义控件的文本颜色设置动画,这两种颜色是从自定义控件的两个 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 是最佳实践吗?

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

感谢您的帮助。

最佳答案

尝试使用绑定(bind),它似乎是这样工作的

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>

关于wpf - 动画文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com