相关文章推荐
温暖的洋葱  ·  await-thenable | ...·  2 周前    · 
独立的松鼠  ·  Sql Server 处理 Json ...·  4 月前    · 
爱喝酒的双杠  ·  86版电视剧《西游记》铁扇公主王凤霞被误认为 ...·  1 年前    · 
朝气蓬勃的圣诞树  ·  Android ...·  1 年前    · 
悲伤的熊猫  ·  购置农机促生产,连州九陂镇四联村保障粮食安全 ...·  2 年前    · 
Code  ›  处于IsEnabled="False“状态的WPF TextBox未应用正确的背景色开发者社区
wpf background
https://cloud.tencent.com/developer/ask/sof/1590758
旅行中的荒野
2 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 处于IsEnabled="False“状态的WPF TextBox未应用正确的背景色

Stack Overflow用户
提问于 2017-03-11 23:40:03
EN

当我的文本框被禁用时,我在给它应用颜色时遇到了问题,

此外,我有数据网格,我用 "#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>

正如你所看到的,这里有一段代码(triger),它说ok,当你是残疾人时,让你的背景色像这样,边刷像这样:

<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”<- colour时,我如何做到这一点呢?

我还必须注意到,如果我改变边界笔刷的颜色,它是有效的。例如,当它们被禁用时,我将它们都设置为textbox,并将边刷设置为红色,并得到以下结果:

​

​

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

谢谢你们,干杯

1 1K 0 票数 0
EN
c#
wpf
triggers
styles
border

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-13 04:31:56

在禁用 ControlTemplate 时,删除将 Background 设置为 SystemColors.ControlBrushKey 的 ControlTemplate 中的 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}}"/>
 
推荐文章
温暖的洋葱  ·  await-thenable | typescript-eslint
2 周前
独立的松鼠  ·  Sql Server 处理 Json 相关技术 - HackerVirus - 博客园
4 月前
爱喝酒的双杠  ·  86版电视剧《西游记》铁扇公主王凤霞被误认为是六小龄童夫人
1 年前
朝气蓬勃的圣诞树  ·  Android 底部导航栏(底部Tab)最佳实践 - 门罗的魔术师 - 博客园
1 年前
悲伤的熊猫  ·  购置农机促生产,连州九陂镇四联村保障粮食安全有“粮”策 - 连州门户网站 - http://www.lianzhou.gov.cn/xxgk/zwdt/zxdt/content/post_15884
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号