WPF XAML动画属性路径不起作用

时间:2012-05-24 15:59:42

标签: wpf xaml animation properties path

我正在尝试设置地铁风格按钮的内容动画。 我的问题是按钮的内容不会改变颜色。 前景色不会改变。

这是我按钮的风格:

    <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"/>

1 个答案:

答案 0 :(得分:0)

你有

Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
Storyboard.TargetName="EllipseInnerContent"
在您的动画中

,其中EllipseInnerContentContentPresenterForeground上没有ContentPresenter属性。

将其更改为ContentControl

此外,当将TextBlock作为控件的内容时,它将继承其所属的页面\用户控件的前景。使用DataTemplate代替为您创建TextBlock,然后它将从按钮继承前景。