ListBox Selected Item Background

前端 未结 2 1486
一生所求
一生所求 2020-12-06 23:44

I\'m trying to change the background of the selected item in a WPF ListBox.

I have attempted to implement a style for it, but for some reason it\'s not being applied

相关标签:
2条回答
  • 2020-12-07 00:26

    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>
    
    0 讨论(0)
  • 2020-12-07 00:44

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