I'm currently implementing a custom style for a button and want to define the different states (eg. Pressed) with a 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>
As you can see I want to change the grid's background property to the color red on the press state, but the following exception is thrown:
First-chance exception at 0x7708210B (KERNELBASE.DLL) in gymlog.exe: 0x40080201: WinRT originate error (parameters: 0x800F1000, 0x00000056, 0x0178E4F4).
If I jump to the specific memory adresse the following gets displayed:
ColorAnimation cannot be used to animate property Background due to incompatible type
How to solve this?
Background
property is not a color. It's a brush, so you can't animate it with ColorAnimation
. Brushes can be animated with ObjectAnimationUsingKeyFrames
. But first you have to create a new brush with the target color (in your case it's red).
You can add SolidColorBrush
to resources in the same place where is your style:
<SolidColorBrush x:Name="RedBrush" Color="Red" />
<!-- And here goes your button style... -->
Then you can use it in object animation.
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootElement"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource RedBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
User contributions licensed under CC BY-SA 3.0