0
votes

I'm using WPFToolkit's AutoCompleteBox, and I need to change the selected item background color, but I can't do it. I can change the font styling, font size, just not the background.

I've tried many solutions from SO, however none of them worked. What I've tried so far:

Change background color for selected ListBox item

Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

Using a trigger to change the selecteditem's background dynamically

<Style  x:Key="myLBStyle" TargetType="ListBoxItem">
        <Setter Property="Background" Value="IndianRed" />
        <Setter Property="Foreground" Value="WhiteSmoke" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="FontSize" Value="22" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="Chartreuse" />
                <Setter Property="FontStyle" Value="Italic" />
                <Setter Property="Foreground" Value="Chartreuse" />
            </Trigger>
        </Style.Triggers>
    </Style>

<local:FocusableAutoCompleteBox x:Name="ACBox" Margin="10,32,10,0"
    Grid.Row="2" FontSize="27" Grid.ColumnSpan="4" Foreground="#FF333333"
    Background="#FF1700FF" BorderThickness="2" TextChanged="_ACBox_TextChanged"
    KeyDown="ACBox_KeyDown" Focusable="True" MinimumPopulateDelay="100"
    MinimumPrefixLength="1" ItemContainerStyle="{DynamicResource ResourceKey=myLBStyle}">

I've also tried overriding system colours:

        <Style  x:Key="myLBStyle" TargetType="ListBoxItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
        </Style.Resources>
        <Setter Property="Background" Value="IndianRed" />
        <Setter Property="Foreground" Value="WhiteSmoke" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="FontSize" Value="22" />
    </Style>

I can successfully set other properties with the trigger. I can set the selecteditem's font italic, bold, larger, smaller, but I cannot change the selected item's background colour.

1

1 Answers

0
votes
<Style  x:Key="myLBStyle" TargetType="ListBoxItem">
    <Setter Property="Background" Value="IndianRed" />
    <Setter Property="Foreground" Value="WhiteSmoke" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="FontSize" Value="22" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter></ContentPresenter>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="true">
            <Setter Property="Background" Value="Chartreuse" />
            <Setter Property="FontStyle" Value="Italic" />
            <Setter Property="Foreground" Value="Chartreuse" />
        </Trigger>
    </Style.Triggers>
</Style>