我有自定义控件,如下所示:
<Style TargetType="local:AnswerButton">
<Setter Property="Background" Value="{StaticResource BlueGradient}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:AnswerButton">
<Grid>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CommonStates">
<vsm:VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background)" Storyboard.TargetName="myBorder">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BlueGradient}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="RightAnswer">
<Storyboard RepeatBehavior="Forever" BeginTime="00:00:00" AutoReverse="True">
<ColorAnimation Duration="0:0:0.6" From="Orange" To="Green" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="myBorder"/>
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Border BorderBrush="Blue" BorderThickness="2" CornerRadius="10">
<Border Name="myBorder" Background="{TemplateBinding Background}" CornerRadius="9" Opacity="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
TextAlignment="Center" VerticalAlignment="Center"
Text="{TemplateBinding Option}" Foreground="Yellow" />
<TextBlock Grid.Column="1"
TextAlignment="Left" VerticalAlignment="Center"
Text="{TemplateBinding Text}" Foreground="White" />
</Grid>
</Border>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我只想改变按钮的状态。首先(默认)哪个按钮具有 BlueGradient 画笔,其次是有动画的按钮(如 RightAnswer 状态)。此代码部分有效。当我将 RightAnswer 状态设置为按钮时,我将其更改为 Normal,然后当我尝试将其设置为 RightAnswer 时,我收到此错误:
无法解析指定对象上的 TargetProperty (Border.Background).(SolidColorBrush.Color)。
我认为问题在于,在一个状态下,我得到Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
了第二个状态Storyboard.TargetProperty="(Border.Background)"
。我试图改变它,但它不起作用。那么我该如何解决这个问题呢?
编辑:
蓝色渐变
<LinearGradientBrush x:Key="BlueGradient"
StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#12449d" Offset="0" />
<GradientStop Color="#0a2554" Offset="1" />
</LinearGradientBrush>