2

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

这是我的按钮样式:

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

1 回答 1

0

你有

Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
Storyboard.TargetName="EllipseInnerContent"

在您的动画中,EllipseInnerContenta在哪里,ContentPresenter并且 . 上没有Foreground属性ContentPresenter

将其更改为ContentControl.

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

于 2012-05-25T06:36:13.180 回答