2013-01-10 15 views
1

之間變化的背景我有一個看起來像這樣的自定義控制:XAML中 - VisualStates - 顏色和動畫

<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狀態按鈕,然後我將其更改爲正常,然後當我試圖將其設置爲RightAnswer我得到這個錯誤:

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

我認爲問題是,在一個國家我Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"和第二Storyboard.TargetProperty="(Border.Background)" 。我試圖改變,但它不工作。那我該如何解決這個問題?

編輯:

BlueGradient

<LinearGradientBrush x:Key="BlueGradient" 
    StartPoint="0,0" EndPoint="1,1"> 
     <GradientStop Color="#12449d" Offset="0" /> 
     <GradientStop Color="#0a2554" Offset="1" /> 
    </LinearGradientBrush> 
+0

你能告訴我們BlueGradient是什麼嗎?它是一個漸變畫筆嗎? – dtm

+0

沒問題,我添加了代碼:) –

回答

2

你有一個LinearGradientBrushBackground財產刷,但你嘗試在它訪問(SolidColorBrush.Color)(本質上,它在鑄造LGB到SCB失敗)。

對於這種情況,您不能使用刷子的資源,因爲畫筆的類型決定了您需要運行的Storyboard。您必須降低一級,並使用Color作爲您的資源。

(目前我使用的不是我的dev的機器,使語法可能有點關閉)

首先,在資源

<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> 
+0

正是我在找的東西。謝謝 :) –