wpf 将禁用的列表框背景更改为灰色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15773669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:26:11  来源:igfitidea点击:

Change disabled listbox background to gray

wpfbackgroundtriggerslistboxcontroltemplate

提问by Nallware

I have a ListBoxwhich 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. shrugsI'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.

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

Here is my 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>

It seems fairly straightforward. I did the same basic process on ComboBoxand TextBoxwith 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.

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

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?

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

Edit: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 MSDNthere 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.

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

采纳答案by sa_ddam213

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

我认为您不需要覆盖ControlTemplate,只需Style.Trigger为我添加一个工作正常。

Example:

例子:

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

回答by Bill Tarbell

sa_ddam213's answer didn't work for me so i thought i'd add what i found i had to do. In my case, i had set transparent background, but when i disabled the box it would turn gray. I wanted to be able to control the background of the listbox when the control was disabled and here's what i found to work.

sa_ddam213 的回答对我不起作用,所以我想我会添加我发现我必须做的事情。就我而言,我设置了透明背景,但是当我禁用该框时,它会变成灰色。当控件被禁用时,我希望能够控制列表框的背景,这就是我发现的工作。

note: For your case, you'd want to change the Transparent color to whatever shade of Gray you want.
note2: This will likely only work if you haven't changed the template for the listbox. (changing the datatemplate is ok).

注意:对于您的情况,您需要将透明颜色更改为您想要的任何灰色阴影。
注意2:这可能只有在您没有更改列表框的模板时才有效。(更改数据模板是可以的)。

<ListBox.Style>
    <Style TargetType="{x:Type ListBox}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
        </Style.Resources>
    </Style>
</ListBox.Style>

回答by Felix Brüll

Both answers didn't really work for me so I found a solution that overwrites the ControlTemplate which works for me:

这两个答案对我都不起作用,所以我找到了一个覆盖 ControlTemplate 的解决方案,它对我有用:

            <Style TargetType="{x:Type ListBox}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBox}">
                            <Grid Width="Auto" Height="Auto">
                                <Border x:Name="Border" 
                                        BorderThickness="1"/>
                                <ScrollViewer Focusable="false" IsTabStop="False" HorizontalScrollBarVisibility="Disabled">
                                    <StackPanel IsItemsHost="true"/>
                                </ScrollViewer>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter TargetName="Border" Property="Border.Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                                </Trigger>
                                <Trigger Property="IsGrouping" Value="true">
                                    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Background" Value="{StaticResource DefaultBackground}"/>
            </Style>

This helped me: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4b033268-864e-488c-b371-80818daf7f71/can-i-override-the-disabled-background-color-for-a-listbox?forum=wpf

这对我有帮助:https: //social.msdn.microsoft.com/Forums/vstudio/en-US/4b033268-864e-488c-b371-80818daf7f71/can-i-override-the-disabled-background-color-for- a-listbox?forum=wpf