无法为ContentControl背景色添加动画效果,但可以使用前景色。

4
我正在尝试为Windows Phone 8中的Listbox动画选择更改。以下动画有效:
<ColorAnimation Storyboard.TargetProperty="(ContentControl.Foreground).(SolidColorBrush.Color)" Duration="00:00:00.25" From="{StaticResource PhoneForegroundColor}" To="{StaticResource PhoneAccentColor}" />

但是下一个不起作用(System.InvalidOperationException: 无法解析指定对象上的TargetProperty (ContentControl.Background).(SolidColorBrush.Color)。)
<ColorAnimation Storyboard.TargetProperty="(ContentControl.Background).(SolidColorBrush.Color)" Duration="00:00:00.25" From="{StaticResource PhoneForegroundColor}" To="{StaticResource PhoneAccentColor}" />

我真的找不出理由。

1
尝试在您的ContentControl上设置显式默认的Background... - nemesv
nemesv:什么鬼???你说得对。但是这是否意味着如果没有设置值,则该属性不存在??? - Ignacio Soler Garcia
问题在于Background默认为null,请参见我的答案。 - nemesv
1个回答

6
您需要明确在ContentControl上设置Background属性的值,以使动画正常工作。

这是因为Background属性的默认值null,因此当动画解析表达式(ContentControl.Background).(SolidColorBrush.Color)时,它无法访问nullColor属性。

而且,它可以与Foreground一起使用,因为Foreground属性的默认值是黑色画刷,因此默认情况下设置为有效的非空值。


1
然后问题在于异常。它应该是一个 NullReferenceException,而不是一些说无法解析属性的东西!我花了大约2个小时才解决这个问题 :( - Ignacio Soler Garcia
2
是的,这里的异常信息相当误导。 - nemesv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接