2012-05-24 127 views
2

我正在嘗試使用地鐵樣式按鈕的內容動畫。 我的問題是,按鈕的內容不會改變顏色。 前景色不會改變。WPF XAML動畫屬性路徑不能正常工作

這裏是我的按鈕的風格:

<Style x:Key="MetroButtonStyle" 
     TargetType="Button"> 
    <Setter Property="MinWidth" 
      Value="40"/> 
    <Setter Property="Width" 
      Value="100"/> 
    <Setter Property="MinHeight" 
      Value="88"/> 
    <Setter Property="HorizontalAlignment" 
      Value="Center"/> 
    <Setter Property="Foreground" 
      Value="White"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border x:Name="AppButton" 
         Width="{TemplateBinding Width}" 
         Height="{TemplateBinding Height}"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" 
                To="1" 
                Storyboard.TargetProperty="(UIElement.Opacity)" 
                Storyboard.TargetName="MouseOverEllipse"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" 
                To="1" 
                Storyboard.TargetProperty="(UIElement.Opacity)" 
                Storyboard.TargetName="PressedEllipse"/> 
            <ColorAnimation Duration="0" 
                To="Black" 
                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" 
                Storyboard.TargetName="EllipseInnerContent"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ColorAnimation Duration="0" 
                To="#7F8D8D8D" 
                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" 
                Storyboard.TargetName="EllipseInnerContent"/> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <StackPanel Orientation="Vertical" 
           HorizontalAlignment="Center"> 
         <Grid Margin="0,14,0,5" 
           HorizontalAlignment="Center" 
           MinWidth="40"> 
          <Ellipse x:Name="PressedEllipse" 
            Fill="{TemplateBinding Foreground}" 
            Opacity="0" 
            Width="40" 
            Height="40"/> 
          <Ellipse x:Name="MouseOverEllipse" 
            Fill="#7F8D8D8D" 
            Opacity="0" 
            Width="40" 
            Height="40"/> 
          <Ellipse Fill="Transparent" 
            Stroke="{TemplateBinding Foreground}" 
            StrokeThickness="2"/> 
          <ContentPresenter x:Name="EllipseInnerContent" 
               Content="{TemplateBinding Content}" 
               HorizontalAlignment="Center" 
               VerticalAlignment="Center"/> 
         </Grid> 
         <TextBlock x:Name="LabelText" 
            TextWrapping="Wrap" 
            HorizontalAlignment="Center" 
            FontFamily="Segoe UI" 
            FontSize="12" 
            Text="{TemplateBinding Tag}" 
            Foreground="{TemplateBinding Foreground}"/> 
        </StackPanel> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

這裏的是我如何使用它。 這不是做的工作:

     <Button Style="{StaticResource MetroButtonStyle}" 
           Tag="Blah"> 
          <TextBlock Text="XXX"/> 
         </Button> 

這工作:

     <Button Style="{StaticResource MetroButtonStyle}" 
           Tag="Blah" 
           Content="XXX"/> 
+0

TextBlock不是控件。 TextBlock是一個FrameworkElement。這就是爲什麼動畫化Control.Foreground屬性不起作用的原因。 – roberther

+0

將TargetProperty更改爲Storyboard.TargetProperty =「(TextBlock.Foreground)。(SolidColorBrush.Color)」不會更改任何內容。它甚至可以工作嗎? – SiriusNik

+0

[here](http://stackoverflow.com/questions/3856780/in-a-buttons-control-template-how-can-i-set-the-color-of-contained-text)是一個類似的問題 – roberther

回答

0

你有

Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" 
Storyboard.TargetName="EllipseInnerContent" 
在你的動畫,其中 EllipseInnerContentContentPresenter

而且也沒有Foreground財產上ContentPresenter

將其更改爲ContentControl

此外,當將TextBlock作爲控件的內容時,它將繼承它所屬的頁面\用戶控件的前景。使用DataTemplate代替爲您創建TextBlock,然後它將繼承按鈕的前景。