简体   繁体   English

WPF ListViewItem 触发器背景

[英]WPF ListViewItem Trigger Background

I have the following simple window:我有以下简单的窗口:

<Window x:Class="ListViewTest.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:ListViewTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView BorderThickness="0">

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="BorderThickness" Value="0"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>

            <ListViewItem Content="Just a test"/>
            <ListViewItem Content="Another test"/>
        </ListView>
    </Grid>
</Window>

So when the item is selected the background should become red and the border thickness should become "0".所以当项目被选中时,背景应该变成红色,边框厚度应该变成“0”。 The thickness works but the background color does not work.厚度有效,但背景颜色无效。 Any idea what I did wrong here?知道我在这里做错了什么吗? It seems all brush related properties can not be applied (eg BorderBrush does also not work)似乎所有画笔相关的属性都无法应用(例如 BorderBrush 也不起作用)

The default template for the ListViewItem is not set up to look at the Background property for the color of the item when its selected. ListViewItem 的默认模板未设置为在选中项目时查看 Background 属性的颜色。

Its set up to look for a solid color brush with a key of "Item.SelectedActive.Background"它设置为寻找带有“Item.SelectedActive.Background”键的纯色画笔

To check how the default style template is set up, right click on one of the items in your list in the designer, select Edit Template > Edit a Copy:要检查默认样式模板的设置方式,请在设计器中右键单击列表中的一项,选择 Edit Template > Edit a Copy: 截图1

It will add some code to the top of your Window xaml under <Window.Resources>它将在 <Window.Resources> 下的 Window xaml 顶部添加一些代码

You'll want to pay particularly close attention to the selected text below:您需要特别注意下面选定的文本: 生成的样式

This is what controls the background color of the selected item.这是控制所选项目的背景颜色的内容。 You can simply change the Color value to Red or whatever color you want for the highlighted color and you're off to the races.您可以简单地将颜色值更改为红色或您想要的突出显示颜色的任何颜色,然后您就可以参加比赛了。

You could create your own template but the built in one is pretty robust already.您可以创建自己的模板,但内置模板已经非常强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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