views:

26

answers:

1

I'm restyling a button, and that includes providing a controlTemplate and also adding some triggers (for mouseenter and mouseleave).

My XAML is as follows:

<Style TargetType="Button">
    <Style.Resources>
        <Color  x:Key="ForeColour" R="128" G="128" B="128" A="255"/>

        <Color x:Key="BackColour" R="211" G="211" B="211" A="255"/>

        <Color x:Key="GlowColour" R="30" G="144" B="255" A="255"/>

        <SolidColorBrush Color="{StaticResource ResourceKey=ForeColour}" x:Key="ForeBrush"/>

        <LinearGradientBrush x:Key="BackBrush" StartPoint="0,1" EndPoint="0,0">
            <GradientStop Color="White" Offset="0.5"/>
            <GradientStop Color="{StaticResource ResourceKey=BackColour}"  Offset="1"/>
        </LinearGradientBrush>

        <Storyboard x:Name="mouseOverText">
            <ColorAnimation Storyboard.Target="{Binding RelativeSource= {RelativeSource Self}" Storyboard.TargetProperty="Foreground" From="{StaticResource ResourceKey=ForeColour}" To="{StaticResource ResourceKey=GlowColour}"/>
        </Storyboard>

    </Style.Resources>

    <Setter Property="Foreground" Value="{StaticResource ResourceKey=ForeBrush}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="8" Background="{StaticResource ResourceKey=BackBrush}">
                        <ContentPresenter x:Name="cnpContent" Opacity="1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <EventTrigger  RoutedEvent="MouseEnter">
            <BeginStoryboard Storyboard="{StaticResource mouseOverText}"/>
        </EventTrigger>
    </Style.Triggers>
</Style>

but when I run it I get the following error on startup:

'Missing key value on 'Storyboard' object.' Line number '88' and line position '31'.

Which corresponds to this line:

<ColorAnimation Storyboard.Target="{Binding RelativeSource= {RelativeSource Self}" Storyboard.TargetProperty="Foreground" From="{StaticResource ResourceKey=ForeColour}" To="{StaticResource ResourceKey=GlowColour}"/>

All I'm trying to do is get the button's foreground to fade to a different colour when the mouse hovers over it. Anybody have any idea where I'm going wrong? I can't seem to fathom it.

+3  A: 

I see three issues.

One is that you are adding the Storyboard to a resource dictionary without specifying a key. I think you want x:Key instead of x:Name.

The next is that by setting Storyboard.Target to a binding with RelativeSource Self, you're setting the target to the ColorAnimation itself. You can actually just leave that attribute off and it will target the Button by default.

Finally, you have the TargetProperty set to Foreground, but you actually want to animate the Color property of the Foreground brush, so you need to use Foreground.Color. Try this:

<Storyboard x:Key="mouseOverText">
    <ColorAnimation
        Storyboard.TargetProperty="Foreground.Color"
        From="{StaticResource ResourceKey=ForeColour}"
        To="{StaticResource ResourceKey=GlowColour}"/>
</Storyboard>
Quartermeister
Thanks very much, can't believe I missed those!
Nick Udell