如何在WPF中更改按钮的前景色?

时间:2016-11-10 18:51:22

标签: wpf visual-studio xaml user-interface

我想在鼠标悬停时更改按钮的文字颜色(前景)。我通过编辑以下代码

成功更改了背景颜色
<Style x:Key="ButtonStyle_black" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="{DynamicResource TextBrush}"/>
        <Setter Property="Background" Value="{DynamicResource GrayBrush10}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderBrush}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper"/>
        <Setter Property="Controls:ControlsHelper.CornerRadius" Value="3"/>
        <Setter Property="FontFamily" Value="{DynamicResource DefaultFont}"/>
        <Setter Property="FontSize" Value="{DynamicResource UpperCaseContentFontSize}"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="MinHeight" Value="25"/>
        <Setter Property="Padding" Value="5,6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border x:Name="Background" Background="{TemplateBinding Background}" CornerRadius="{Binding (Controls:ControlsHelper.CornerRadius), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{x:Null}" CornerRadius="{Binding (Controls:ControlsHelper.CornerRadius), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        <Border x:Name="DisabledVisualElement" Background="{DynamicResource ControlsDisabledBrush}" CornerRadius="{Binding (Controls:ControlsHelper.CornerRadius), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" IsHitTestVisible="False" Opacity="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        <Controls:ContentControlEx x:Name="PART_ContentPresenter" ContentCharacterCasing="{Binding (Controls:ControlsHelper.ContentCharacterCasing), RelativeSource={RelativeSource TemplatedParent}}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                        </Trigger>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ButtonMouseOverBorderBrush}"/>
                            <Setter Property="BorderThickness" TargetName="Border" Value="2"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" TargetName="DisabledVisualElement" Value="0.7"/>
                            <Setter Property="Opacity" TargetName="PART_ContentPresenter" Value="0.3"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

但编辑此模板以进行前景色更改无效。

1 个答案:

答案 0 :(得分:1)

您可以使用样式

来完成此操作
   <Window.Resources>
        <Style x:Key="ButtonCancel" TargetType="{x:Type Button}">
            <Setter Property="Foreground">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Red" Offset="0"/>                      
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property ="IsMouseOver" Value="True">
                    <Setter Property= "Foreground"  >
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="Green" Offset="0"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Style="{DynamicResource ButtonCancel}"   FontSize="18" FontWeight="Bold" Width="144" Name="btnCancel"
                HorizontalAlignment="Right" Grid.Row="1"  Grid.Column="1" Margin="0,73.5,0,55.5"> this is test
        </Button>
    </Grid>