为选定的 ListBoxItem 设置样式 [英] Styling a Selected ListBoxItem

查看:28
本文介绍了为选定的 ListBoxItem 设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想为 ListBoxItem 设置样式,使其在选择时具有蓝色背景和白色文本.我在网上看了很多答案,他们似乎都给出了相互矛盾的意见,到目前为止,没有一个对我有用.这是我的 ListBox:

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>

然而,这不会以任何方式设置 ListBoxItem 的样式.简而言之,我的问题是对选定的 ListBoxItem 进行样式设置的最简单方法,因此 ListBox 如下所示:

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:

推荐答案

此解决方案在 Windows 10 上不起作用,这意味着它根本不再适合,继续前进.谢谢,微软.

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

据我所知,这意味着替换 ListBoxItem 控件模板.解决该案例的两个问题:

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.列表框项目样式
  2. https://stackoverflow.com/a/35810145/424129

<小时>

您不能以这种方式更改所选项目的背景颜色;ListBoxItemBackground 属性不会因选择状态而改变.取而代之的是,ListBoxItem 的默认控件模板使用 Background 表示未选中,并且有一个触发器,它用 {DynamicResource {x:当 IsSelectedtrue 时,静态 SystemColors.HighlightBrushKey}}.


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.

可以对你的DataTemplate的子元素做同样的事情,或者你可以替换Template,但只是覆盖资源更容易反而.

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>

这篇关于为选定的 ListBoxItem 设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆