Disabling Listbox is not changing background color

2019-06-21 22:57发布

问题:

I have this simple style that does not change the ListBox Background when the ListBox is disabled:

<Style TargetType="ListBox" >                    
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="Orange"/>
        </Trigger>
    </Style.Triggers>
</Style>

Snoop does not help me on this one and I cannot figure a simple way without overriding templates. Anyone have a simple way to get this working? TIA.

回答1:

The only way to do this is by overriding the template



回答2:

You can change the background color of the listbox itself when the listbox is disabled by simply changing the colors that the built-in template is using. You can do this via style resources. Just paste the below code into your Listbox element and the background will be transparent when you disable the box.

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

It is also very common to want to modify the background color of a single item when it's highlighted and when the box loses focus.. to change these, you can reference this post: https://stackoverflow.com/a/7298301/1721136