1

我有自定义控件,如下所示:

<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>
4

1 回答 1

4

您拥有 aLinearGradientBrush作为Background属性的刷子,但您尝试访问(SolidColorBrush.Color)它(本质上它无法将 LGB 转换为 SCB)。

对于这种情况,您不能使用画笔资源,因为画笔类型决定了Storyboard您需要运行的资源。您必须降低一级并Color用作您的资源。

(目前我没有使用我的开发机器,所以语法可能有点不对)

一、在资源上

<Color x:Key="ColorBlueFirstStop">#12449d</Color>
<Color x:Key="ColorBlueSecondStop">#0a2554</Color>

然后,在模板中

<Border Name="myBorder" CornerRadius="9" Opacity="1">
    <Border.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
            <GradientStop Color="{StaticResource ColorBlueFirstStop}" Offset="0" />
            <GradientStop Color="{StaticResource ColorBlueSecondStop}" Offset="1" />
        </LinearGradientBrush>
   ......
</Border>

最后在故事板中

<Storyboard RepeatBehavior="Forever" BeginTime="00:00:00" AutoReverse="True">
    <ColorAnimation Duration="0:0:0.6" 
                    From="Orange" 
                    To="Green" 
                    Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="myBorder"/>
    <ColorAnimation Duration="0:0:0.6" 
                    From="Orange"  
                    To="Green" 
                    Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="myBorder"/>
</Storyboard>
于 2013-01-10T20:47:12.590 回答