如何将WPF stype应用于按钮模板的子项

时间:2017-06-07 13:53:58

标签: wpf styles google-material-icons

我是WPF的新手,我想更改为按钮模板的文本和图标的颜色。但似乎我只能改变不透明度。 我想我应该将这种风格应用于按钮的孩子,但我不知道如何。

以下是模板:

<Button x:Name="btnApp1" Width="56" Height="66" Margin="0,0,0,0" Style="{StaticResource AppButton}">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <iconPacks:PackIconMaterial Kind="StarOutline"  Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="#FFFFFFFF" />
                    <TextBlock x:Name="tButton" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="#FFFFFFFF" FontWeight="Bold">PVIE</TextBlock>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>

这是风格:

<Style x:Key="AppButton" TargetType="{x:Type Button}">

    <Setter Property="Opacity" Value="0.25" />
    <Setter Property="Foreground" Value="#FFFF9966" />

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0:0.3" />
                        <!--<ColorAnimation Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="Green" Duration="0:0:0:0.3" />-->

                        <ColorAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                                        Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0" Value="Green" />
                        </ColorAnimationUsingKeyFrames>

                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.25" Duration="0:0:0:0.3" />
                        <!--<ColorAnimation Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="White" Duration="0:0:0:0.3" />-->

                        <ColorAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                                        Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0" Value="White" />
                        </ColorAnimationUsingKeyFrames>

                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>

</Style>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用Foreground TextBlock绑定IconControlTemplate的{​​{1}}属性:

{TemplateBinding}