IsEnabled="False" 状态下的 WPF TextBox 未应用正确的背景颜色

WPF TextBox in IsEnabled="False" state is not applying correct background color

我在禁用文本框时遇到问题,

实际上我有数据网格,我用 "#E0E4E5" 颜色给行着色。当我的文本框被禁用时,我想保留它的颜色,就像行的颜色是 ("#E0E4E5").

接下来我做了什么:

我将 属性 设置为行 AlternatingRowBackground="#E0E4E5" 然后我将此颜色作为行的背景颜色。

之后我做了下一步,我为我的文本框制作了样式,因为 wpf 中的默认样式看起来不太好,它有一些阴影等,所以这是我为文本框自定义的样式:

  <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="#E0E4E5" />
                <Setter Property="BorderBrush" Value="#E0E4E5" />
                <Setter Property="BorderThickness" Value="1.5" />
            </Trigger>
        </Style.Triggers>

        <Setter Property="BorderBrush" Value="#0091EA"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="PART_ContentHost"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

正如你们所看到的,有一段代码(触发器)说好的,当你被禁用时,让你的背景颜色像这样,边框刷像这样:

<Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="#E0E4E5" />
                <Setter Property="BorderBrush" Value="#E0E4E5" />
                <Setter Property="BorderThickness" Value="1.5" />
            </Trigger>
        </Style.Triggers>

下面是它看起来如何的示例:

因为可以看到我对它们“#E0E4E5”应用了相同的颜色,但很明显它们是不同的,所以伙计们我是怎么做到的,所以当我的文本框被禁用时它变成了“#E0E4E5”<-颜色..

而且我还必须注意,如果我更改边框刷的颜色,它会起作用。例如,我将它们的文本框和边框刷设置为红色,当它们被禁用时,我得到这个:

所以边框笔刷变了,但是背景没有。

谢谢大家, 干杯

删除 ControlTemplate 中的 Setter,当 TextBox 被禁用时将 Background 设置为 SystemColors.ControlBrushKey

<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="#E0E4E5" />
            <Setter Property="BorderBrush" Value="#E0E4E5" />
            <Setter Property="BorderThickness" Value="1.5" />
        </Trigger>
    </Style.Triggers>

    <Setter Property="BorderBrush" Value="#0091EA"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>