VisualStateManager thrown exception


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">
        <ControlTemplate TargetType="Button">
            <Grid x:Name="RootElement" Background="{TemplateBinding Background}">
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="Pressed">
                                <ColorAnimation Storyboard.TargetName="RootElement"

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?

asked on Stack Overflow Mar 19, 2015 by neuronalbit

1 Answer


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">
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootElement"
            <DiscreteObjectKeyFrame KeyTime="0" 
                                    Value="{StaticResource RedBrush}"/>
answered on Stack Overflow Mar 19, 2015 by Łukasz Rejman • edited Mar 19, 2015 by Łukasz Rejman

User contributions licensed under CC BY-SA 3.0