【问题标题】:How to change foreground color of button in WPF?如何更改 WPF 中按钮的前景色?
【发布时间】:2017-03-24 21:58:54
【问题描述】:

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

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

但是编辑这个模板来改变前景色是行不通的。

【问题讨论】:

    标签: wpf visual-studio xaml user-interface


    【解决方案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>
    

    【讨论】:

    • sry 我不具体,我的错,我想改变鼠标悬停事件的前景。谢谢。
    • @user3086871 检查更新的答案,如果有帮助则标记为答案
    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 2018-01-06
    • 1970-01-01
    • 2020-12-30
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多