Change disabled listbox background to gray

前端 未结 3 1083
孤街浪徒
孤街浪徒 2020-12-20 22:54

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

相关标签:
3条回答
  • 2020-12-20 23:04

    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>
    
    0 讨论(0)
  • 2020-12-20 23:04

    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

    0 讨论(0)
  • 2020-12-20 23:13

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

    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>
    
    0 讨论(0)
提交回复
热议问题