You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF按钮MouseOver状态Foreground不生效问题求助

问题:WPF Button MouseOver状态Foreground不生效

我在WPF中修改Button的MouseOver状态样式时,Background、BorderBrush属性都能按预期变化,但Foreground属性始终无法生效。使用的是资源字典中的如下Style:

<Style x:Key="RoundedCorners" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource ControlDefaultBackground}" />
    <!--<Setter Property="BorderBrush" Value="{StaticResource ControlDefaultBorderBrush}" />-->
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="true"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        CornerRadius="10">
                    <ContentPresenter x:Name="contentPresenter"
                                      Focusable="False"
                                      Margin="{TemplateBinding Padding}"
                                      RecognizesAccessKey="True"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Center" 
                                      />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsDefaulted" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ControlMouseOverBorderBrush}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" TargetName="border" Value="Gray" />
                        <Setter Property="BorderBrush" TargetName="border" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
                        <Setter Property="Foreground" Value="Black"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
                        <Setter Property="BorderBrush" TargetName="border" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource ControlBrightDefaultBackground}" />
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ControlBrightDefaultBorderBrush}" />
                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

试过一些建议方案,发现仅在Windows.Resources范围内生效,无法与ControlTemplate样式配合使用。我知道ControlTemplate存在层级关系,但对此缺乏直观理解。调试显示Foreground颜色已正确设置,但界面上并未显示出对应的效果。

之前处理DataGridHeader的类似样式问题时,通过添加<ContentPresenter HorizontalAlignment="Center"/>解决了问题,但Foreground并非ContentPresenter的属性,无法照搬该方案。当时的DataGridHeader样式代码如下:

<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="SlateBlue"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
            <Border Background="#FF2D2D2D">
                <Border BorderThickness="1"
                    Background="SlateBlue"
                    Padding="10,0,0,0"
                    CornerRadius="0,10,0,0">
                    <ContentPresenter HorizontalAlignment="Center"/>
                </Border>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

内容的提问来源于stack exchange,提问作者ferday

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 14:37:34