我目前正在实现一个按钮的自定义样式,并希望定义不同的状态(例如。(按下)用VisualStateManager。
<Style x:Name="customStyle" TargetType="Button">
<Setter Property="ClickMode" Value="Release"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="RootElement" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="RootElement"
Storyboard.TargetProperty="Background"
To="Red"
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>如您所见,我希望将网格的背景属性更改为按下状态下的红色,但引发以下异常:
一次机会异常在0x7708210B (KERNELBASE.DLL)在gymlog.exe: 0x40080201: WinRT发起错误(参数: 0x800F1000,0x00000056,0x0178E4F4)。
如果我跳转到特定的内存中,将显示以下内容:
由于类型不兼容,ColorAnimation不能用于动画属性背景。
如何解决这个问题?
发布于 2015-03-19 12:58:41
Background属性不是颜色。这是一个画笔,所以你不能用ColorAnimation动画它。画笔可以用ObjectAnimationUsingKeyFrames动画化。但首先,您必须创建一个新的刷与目标颜色(在您的情况下,它是红色)。
您可以在您的样式相同的位置将SolidColorBrush添加到资源中:
<SolidColorBrush x:Name="RedBrush" Color="Red" />
<!-- And here goes your button style... -->然后您可以在对象动画中使用它。
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootElement"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource RedBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>https://stackoverflow.com/questions/29143171
复制相似问题