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

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

问题描述

我有一个的ListBox ,我需要变灰被禁用时。每个用户请求时,它是不够的,禁用它,但它也有出现不同。的耸耸肩的我看着在其他几个地方,随后的例子中,它好像它应该工作,但事实并非如此。下面是一些例子我看了看:<一href=\"http://stackoverflow.com/questions/9084425/disabling-listbox-is-not-changing-background-color-in-style\">Example1, <一href=\"http://stackoverflow.com/questions/2594714/change-background-color-of-disabled-listbox-in-windows-classic-theme\">Example2.

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.

下面是我的code:

<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>

这似乎相当简单。我做了组合框文本框成功相同的基本过程。谁能帮我看看我在哪里code是错误的?如何正确地做到这一点的一个例子将是巨大的。上面列出的第一个例子似乎正是我需要的,但正确答案是要做到这一点的唯一方法是通过重写模板,这并不能帮助我。

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中的一部分。由于没有造型,当我禁用列表框,它被冻结,但没有文字可见,并且拥有默认背景。当我尝试应用属性=背景VALUE =浅灰色似乎被隐藏(即没有什么可见的)。如果有人知道它为什么可以这样做,或如何完成我的任务,我会AP preciate的帮助。

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.

推荐答案

我不认为你需要重写控件模板,只需添加 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天全站免登陆