Disabling Listbox is not changing background color in style

一个人想着一个人 提交于 2019-12-10 13:44:30

问题


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



来源:https://stackoverflow.com/questions/9084425/disabling-listbox-is-not-changing-background-color-in-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!