WPF, set the background of the Selected ListBox item RRS feed

  • Question

  • Hi, I'm trying to set the background of the selected item if a Listbox. 

    This is my first attempt: 

            <ListBox>             <ListBoxItem>first</ListBoxItem>             <ListBoxItem>second</ListBoxItem>             <ListBoxItem>third</ListBoxItem>                      <ListBox.Resources>                     <Style TargetType="{x:Type ListBoxItem}">                     <Style.Triggers>                             <Trigger Property="IsSelected" Value="True">                                 <Setter Property="Height" Value="60" />                                 

    <!--doesn't work-->                                 <Setter Property="Background" Value="violet"/>                                                                             <Setter Property="Foreground" Value="red"/>                             </Trigger>                         </Style.Triggers>                     </Style>             </ListBox.Resources>


    It sets three properties: Height, Background, Foreground. It works for Height and Foreground, not for Background.

    I've looked up the web and the solutions are essentially two:

    1) change the system colors at the component level. It seems to be a trick more than a solution, and has its issues too. 

    2) retemplate the ListBoxItem, that's quite complex. 

    Is there an 'official' solution, that is also reasonably simple?

    Are there plans to simplify this? What are other cases of proprerties that don't work the simple way? I'd better know them in advance. 

    Why Background behaves differently from the other properties? 

    Where is this 'exception' mentioned in the documentation?

    Thanks in advance


    • Edited by AgostinoX Wednesday, August 12, 2020 9:56 PM
    • Moved by CoolDadTx Thursday, August 13, 2020 1:42 PM WPF related
    Wednesday, August 12, 2020 9:53 PM

All replies

  • Wednesday, August 12, 2020 9:58 PM
  • Hi AgostinoX,

    Thank you for posting here.

    This is an example of redesigning ControlTemplate, you can refer to it.

                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Template">
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <Border x:Name="Bd"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                Padding="{TemplateBinding Padding}"
                                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                <Condition Property="Selector.IsSelectionActive"
                                                Value="False" />
                                                <Condition Property="IsSelected"
                                                Value="True" />
                                            <Setter Property="Background"
                                        Value="DarkOrange" />
                                                <Condition Property="Selector.IsSelectionActive"
                                                Value="True" />
                                                <Condition Property="IsSelected"
                                                Value="True" />
                                            <Setter Property="Background" TargetName="Bd" Value="Orange" />
                                            <Setter Property="Height" Value="65"/>
                                            <Setter Property="Foreground" Value="Red"/>

    >>Are there plans to simplify this? What are other cases of proprerties that don't work the simple way?

    These questions are best raised in Microsoft Q&A as DA924x said.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 13, 2020 8:10 AM