将禁用的列表框背景更改为灰色 [英] Change disabled listbox background to gray

查看:18
本文介绍了将禁用的列表框背景更改为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBox,当它被禁用时我需要将它变灰.每个用户请求仅禁用它是不够的,但它也必须以不同的方式出现.耸肩 我已经查看了其他几个地方并遵循了示例,似乎它应该起作用,但它没有.以下是我查看的一些示例:Example1示例 2.

I have a ListBox which I need to gray out when it's disabled. Per user request it's not enough to disable it, but it also has to appear differently. shrugs I've looked in several other places and followed the examples, and it seems as if it should work, but it does not. Here are some examples I looked at: Example1, Example2.

这是我的代码:

<Style x:Key="ListBoxStyle" TargetType="ListBox">
 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="BorderBrush" Value="Black"></Setter>
                        <Setter Property="Foreground" Value="LightGray"></Setter>
                            <Setter Property="Background" Value="LightGray"></Setter>
                        <Setter Property="BorderBrush" Value="LightGray"></Setter>
                    </Trigger>
                 </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>                 
 </Style>

看起来相当简单.我在 ComboBoxTextBox 上做了相同的基本过程并成功.谁能帮我看看我的代码哪里错了?如何正确执行此操作的示例会很棒.上面列出的第一个示例似乎正是我所需要的,但正确的答案是唯一的方法是覆盖模板",这对我没有帮助.

It seems fairly straightforward. I did the same basic process on ComboBox and TextBox with success. Can anyone help me see where my code is wrong? An example of how to do it correctly would be great. The first example listed above seemed to be exactly what I needed, but the correct answer was "The only way to do this is by overriding the template" which doesn't help me.

我已经尝试了几种简单的解决方案.由于我们正在使用几个不同的资源词典,因此其他风格可能会影响这一点.有谁知道什么可能是跟踪此问题的好方法?

I've already tried several simple solutions. It's possible some other style may be affecting this because we're working with a couple different Resource Dictionaries. Does anybody know what may be a good way to track this down?

我对整个解决方案进行了搜索,唯一使用 ListBox 的地方是我的部分,唯一的样式是我设置的样式.根据 MSDN 没有列表框的部分",所以我不可能在为其他控件设置样式的过程中无意中对 ListBox 的一部分进行了样式设置.在没有样式的情况下,当我禁用 ListBox 时,它会被冻结但没有文本可见,并且具有默认背景.当我尝试应用 Property="Background" Value="LightGray" 时,它似乎被隐藏了(即什么都不可见).如果有人知道为什么会这样做或如何完成我的任务,我将不胜感激.

I did a search on the entire solution and the only place ListBox is being used is my portion and the only place it's being styled is the styles I've set. According to MSDN there are no "parts" of a ListBox, so it's not possible I inadvertently styled part of the ListBox in the process of styling for some other control. With no styling, when I disable the ListBox, it is frozen but visible with no text, and has a default background. When I try to apply the Property="Background" Value="LightGray" it seems to be hidden (i.e. nothing is visible). If anybody knows why it may be doing this or how to accomplish my task, I'd appreciate the help.

推荐答案

我认为您不需要覆盖 ControlTemplate,只需添加一个 Style.Trigger 即可对我来说很好.

I don't think you need to override the ControlTemplate, just adding a Style.Trigger worked fine for me.

示例:

   <ListBox>
        <ListBox.Style>
            <Style TargetType="{x:Type ListBox}">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="LightGray" />
                        <Setter Property="Background" Value="LightGray" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>

这篇关于将禁用的列表框背景更改为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆