动画文本颜色
我需要在两种颜色之间设置自定义控件的文本颜色动画,这些颜色是从自定义控件的两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 Binding,它似乎像这样工作
这是一个 xaml 示例
Tried with using a Binding and it seems to be working like this
Here's a xaml example