ListBox Selected Item Background

时间秒杀一切 提交于 2019-11-28 01:41:34

You specify the SelectedItem Background for a ListBox with the SystemColors.HighlightBrushKey (focused) and SystemColors.ControlBrushKey (not focused)

<Style TargetType="{x:Type ListBox}">
    <Style.Resources> 
        <!-- Background of selected item when focussed --> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                         Color="White"/> 
        <!-- Background of selected item when not focussed --> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                         Color="White" /> 
    </Style.Resources>
    <!--...-->
</Style>
Martin Grundy

For anyone searching for this in the future, the accepted answer doesn't actually apply the colour when control is not focused for me. The following should be used instead which appears to work as intended.

<Style TargetType="ListBox">
    <Style.Resources>
        <!-- Background of selected item when focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFFFB733" />
        <!-- Background of selected item when not focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FFFFB733"/>
    </Style.Resources>
</Style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!