Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change disabled listbox background to gray

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.

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

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

like image 644
Nallware Avatar asked Apr 02 '13 20:04

Nallware


2 Answers

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.

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

<ListBox.Style>
    <Style TargetType="{x:Type ListBox}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
        </Style.Resources>
    </Style>
</ListBox.Style>
like image 194
Bill Tarbell Avatar answered Sep 20 '22 16:09

Bill Tarbell


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

            <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

like image 27
Felix Brüll Avatar answered Sep 22 '22 16:09

Felix Brüll