WPF中如何给自定义依赖项属性设置动画效果,该属性是Brush类型

博轩 2018-03-19 10:19:53
在.cs中添加了自定义属性:
public static DependencyProperty CurrentForegroundProperty =
DependencyProperty.Register("CurrentForeground", typeof(Brush), typeof(Turnout), new PropertyMetadata((Brush)m_fwkElement.TryFindResource("@InitColor"), CurrentForegroundChanged));
public Brush CurrentForeground //[当前前景色]
{
get { return (Brush)GetValue(CurrentForegroundProperty); }
set { SetValue(CurrentForegroundProperty, value); }
}
private static void CurrentForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Brush NewBrush = (Brush)e.NewValue;
Brush OldBrush = (Brush)e.OldValue;

Turnout turnout = d as Turnout;
turnout.HistoryForeground = OldBrush;
turnout.CurrentForeground = NewBrush;
}

在.XAML对其前景色进行了绑定:
<Style TargetType="{x:Type local:Turnout}">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=CurrentForeground,Mode=TwoWay}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Template" Value="{StaticResource TurnoutTemplate}"/>
</Style>

在模板TurnoutTemplate中对自定义属性进行动画处理:
<Storyboard>
<ColorAnimationUsingKeyFrames RepeatBehavior="Forever" BeginTime="00:00:00" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)">
<DiscreteColorKeyFrame Value="Red" KeyTime="00:00:0.3" />
<DiscreteColorKeyFrame Value="Transparent" KeyTime="00:00:0.6" />
</ColorAnimationUsingKeyFrames>
</Storyboard>

在调试时会报异常:无法对不可变对象实例上的“(0).(1)”进行动画处理。
这是怎么回事儿啊?求大神帮帮忙,动画效果应该怎么写啊?
...全文
975 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Veary 2018-03-30
  • 打赏
  • 举报
回复
引用 4 楼 qq_38019697 的回复:
[quote=引用 3 楼 u011785544 的回复:] [quote=引用 2 楼 qq_38019697 的回复:] [quote=引用 1 楼 duanzi_peng 的回复:] 在xaml中动画不能改变brush的值!要想改变值 在后台事件中手动编码处理storyboard/xxxxKeyFrames。
后台事件要怎么写啊?还是用ColorAnimationUsingKeyFrames和DiscreteColorKeyFrame吗?还望说的详细些,或者有参考示例吗?谢谢版主[/quote] [/quote] 这个是怎么实现的啊?还望赐教,多谢![/quote] 我自己写着玩的 随便看看吧 可能有帮助
博轩 2018-03-29
  • 打赏
  • 举报
回复
引用 3 楼 u011785544 的回复:
[quote=引用 2 楼 qq_38019697 的回复:] [quote=引用 1 楼 duanzi_peng 的回复:] 在xaml中动画不能改变brush的值!要想改变值 在后台事件中手动编码处理storyboard/xxxxKeyFrames。
后台事件要怎么写啊?还是用ColorAnimationUsingKeyFrames和DiscreteColorKeyFrame吗?还望说的详细些,或者有参考示例吗?谢谢版主[/quote] [/quote] 这个是怎么实现的啊?还望赐教,多谢!
博轩 2018-03-19
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
在xaml中动画不能改变brush的值!要想改变值 在后台事件中手动编码处理storyboard/xxxxKeyFrames。
后台事件要怎么写啊?还是用ColorAnimationUsingKeyFrames和DiscreteColorKeyFrame吗?还望说的详细些,或者有参考示例吗?谢谢版主
exception92 2018-03-19
  • 打赏
  • 举报
回复
在xaml中动画不能改变brush的值!要想改变值 在后台事件中手动编码处理storyboard/xxxxKeyFrames。
Veary 2018-03-19
  • 打赏
  • 举报
回复
引用 2 楼 qq_38019697 的回复:
[quote=引用 1 楼 duanzi_peng 的回复:]
在xaml中动画不能改变brush的值!要想改变值 在后台事件中手动编码处理storyboard/xxxxKeyFrames。


后台事件要怎么写啊?还是用ColorAnimationUsingKeyFrames和DiscreteColorKeyFrame吗?还望说的详细些,或者有参考示例吗?谢谢版主[/quote]

8,731

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧