1

尝试设置 a 的背景时,Border出现以下异常:

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

您可以使用以下 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>

将其放入构造函数中进行测试:

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

知道我做错了什么吗?当我将 ButtonBorder 的 ColorAnimation 行放在注释中时,它将起作用。所以它很难找到/设置 的Background属性Border,但Border确实有一个Background属性,不是吗?

4

1 回答 1

4

问题是该Background属性未设置为SolidColorBrush.

将以下内容添加到Border应该可以解决问题:

<Border.Background>
    <SolidColorBrush />
</Border.Background>
于 2012-08-10T16:45:58.137 回答