【问题标题】:BackgroundColor of a disabled TextBox已禁用文本框的背景颜色
【发布时间】:2011-04-26 19:55:04
【问题描述】:

我有一个通过 ControlTemplate 定义的 TextBox。由于 ControlTemplate,当 IsEnabled 属性设置为 false 时,TextBox 不再自动变灰。

为了提供此功能,我在 ControlTemplate 中使用了以下触发器:

<Trigger Property="IsEnabled" Value="False">                            
    <Setter Property="Foreground" Value="{x:Static SystemColors.GrayTextBrush}" />
</Trigger>

这很好用。但是我还必须设置 BackgroundColor 但我没有在SystemColors 中找到相应的条目。哪个条目是禁用控件(文本框)背景的正确条目?除了 SystemColors 还有其他来源吗?

我不想使用固定值。例如设置Background="#f4f4f4",因为我担心在某些环境中,禁用背景有另一个值,然后我的控件看起来不应该甚至不可读(例如,如果 GrayTextBrush 的值接近 #f4)。

【问题讨论】:

    标签: .net wpf


    【解决方案1】:

    以下 StackOverflow 问题可能会有所帮助:

    Visual guide to System.Windows.SystemColors

    编辑:

    我做了一些额外的调查,并查看了 Microsoft 提供的标准 XAML 样式(请参阅Where can I download Microsoft's standard WPF themes from?)。您可以准确地看到哪些SystemColors 值用于各种控件。

    例如,这里是ComboBox的控制模板的sn-p:

    <Trigger Property="IsEnabled" Value="false">
        ...
        <Setter
            TargetName="Bd"
            Property="Background"
            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        ...
    </Trigger>
    

    Microsoft 使用SystemColors.ControlBrushKey 作为禁用ComboBox 的背景颜色。

    【讨论】:

    • +1 谢谢。有趣的帖子,但是我没有找到我的问题的答案。我错过了什么吗?
    • 您希望使用 SystemColors 值而不是使用固定值作为背景,对吗?通过查看默认的 XAML 样式,Microsoft 似乎使用 SystemColors.ControlBrushKey 值作为禁用控件的背景颜色。此颜色应与 SystemColors.GrayTextBrush 形成对比,因此当控件被禁用时,用户将能够读取该值。
    • +1 以获得很好的答案。注意 DynamicResource 的使用,如果用户在应用程序运行时更改 Windows 主题中的颜色,应用程序会自动更改颜色。
    【解决方案2】:

    Win7 aero SystemColors.ControlBrushKey 的十六进制值为 F0F0F0。 不是 F4F4F4。 所以那个不正确,不知道用哪个,所以我打算用F4F4F4。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-27
      • 2010-12-29
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      相关资源
      最近更新 更多