locked
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>

    </ListBox>



    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.

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

    >>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,

    Timon


    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