简体   繁体   中英

Styling a Selected ListBoxItem

EDIT:

<Window x:Class="test_project.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:test_project"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <ListBox>
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />

                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow" Opacity="0.6" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
            </ListBox.Resources>
            <ListBox.Items>
                <ListBoxItem Content="Hello"/>
                <ListBoxItem Content="Hello"/>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>

I want to Style a ListBoxItem so that it has a blue background with white text when selected. I've looked at many answers online and they all seem to give contradicting opinions, none of which have worked for me so far. Here is my ListBox :

<ListBox Grid.Row="1" Margin="5" ItemContainerStyle="{StaticResource BlueItemStyle}"
            BorderBrush="#06658D" 
            ItemsSource="{Binding UsersView}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This is the Style that I have used so far and it achieves nothing:

<!--ListBoxItem-->
<Style TargetType="{x:Type ListBoxItem}" x:Key="BlueItemStyle">
    <Setter Property="Height" Value="40"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="#06658D"/>
        </Trigger>
    </Style.Triggers>
</Style>

However this does not Style the ListBoxItem in any way. Simply put, my question would be the simplest way to Style a selected ListBoxItem so the ListBox looks like this:

在此处输入图片说明

This solution does not work on Windows 10, which means it's no longer suitable at all, going forward. Thanks, Microsoft.

To the best of my knowledge, that's going to have to mean replacing the ListBoxItem control template. Two questions addressing that case:

  1. WPF. ListBox item style
  2. https://stackoverflow.com/a/35810145/424129

You can't change the selected item background color that way; the Background property of the ListBoxItem doesn't change for the selection state. Instead, ListBoxItem 's default control template uses Background for unselected, and has a trigger which replaces the actual background brush of some template child with {DynamicResource {x:Static SystemColors.HighlightBrushKey}} when IsSelected is true .

You could do the same with a child of your DataTemplate , or you could replace the Template but it's easier just to override the resource instead.

You can override the same resource globally as well, for a consistent look.

<ListBox 
    Grid.Row="1" 
    Margin="5" 
    ItemContainerStyle="{StaticResource BlueItemStyle}"
    BorderBrush="#06658D" 
    ItemsSource="{Binding UsersView}"
    >
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#06658D" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />

        <!-- 
        The default inactive selection color in Win7 is much too pale for my taste; 
        our older users are unable to distinguish it from white on most monitors. 
        -->
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#06658D" Opacity="0.6" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM