Can't set Border Background in Silverlight StoryBoard ColorAnimation

1.7k views Asked by At

When trying to set the background of a Border, I get the following exception:

Cannot resolve TargetProperty (Border.Background).(SolidColorBrush.Color) on specified object.

You can easily reproduce this with the following Xaml:

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Style x:Key="LinkStyle" TargetType="HyperlinkButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="HyperlinkButton">
                        <Grid x:Name="ButtonGrid" Cursor="{TemplateBinding Cursor}" Background="Transparent">
                            <Border x:Name="ButtonBorder" CornerRadius="10 10 0 0">
                                <ContentControl x:Name="LinkContent" Content="{TemplateBinding Content}"/>
                            </Border>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="LinkStates">
                                    <VisualState x:Name="ActiveLink">
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="ButtonBorder" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="#6F666ECC"  Duration="0:0:0" />
                                            <ColorAnimation Storyboard.TargetName="LinkContent" Storyboard.TargetProperty="(ContentControl.Foreground).(SolidColorBrush.Color)" To="#FFFFFFFF"  Duration="0:0:0" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <HyperlinkButton x:Name="TheLink" Content="Click" Style="{StaticResource LinkStyle}" />
    </Grid>
</UserControl>

Put this in the constructor to test it:

TheLink.Click += (s, e) => VisualStateManager.GoToState((HyperlinkButton)s, "ActiveLink", true);

Any idea on what I'm doing wrong? It will work when I put the line for ColorAnimation of the ButtonBorder in comment. So it has trouble finding/setting the Background property of Border, but Border does have a Background property, no?

1

There are 1 answers

0
Phil Freeman On BEST ANSWER

The problem is that the Background property is not set to an instance of SolidColorBrush.

Adding the following to the Border should do the trick:

<Border.Background>
    <SolidColorBrush />
</Border.Background>