You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WPF问题:ListViewItem无法修改背景颜色,求排查方案

解决WPF ListViewItem背景/边框样式不生效的问题

问题根源在于WPF的ListViewItem默认控件模板中,选中状态的背景是通过VisualStateManager管理的,而非直接绑定到Background属性。你在Style里设置的Background和选中Trigger会被模板内的状态样式覆盖,所以无法生效;而Foreground在默认模板中是直接绑定到控件属性的,所以修改有效。

要解决这个问题,你需要重写ListViewItem的ControlTemplate,将背景、边框的控制权拿到自己的Style里。以下是修改后的完整代码:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Margin" Value="10, 5, 10, 0"/>
            <Setter Property="Height" Value="67" />
            <Setter Property="Width" Value="500"/>
            <Setter Property="Background" Value="#FFD9D9D9"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border x:Name="Bd" 
                                CornerRadius="20"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Bd" Property="Background" Value="Red"/>
                                <Setter TargetName="Bd" Property="BorderBrush" Value="DarkRed"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Bd" Property="Background" Value="#FFE6E6E6"/>
                            </Trigger>
                            <!-- 可选:添加其他状态的样式,比如获得焦点、禁用等 -->
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

关键修改点说明:

  • 新增TemplateSetter,重写了ListViewItem的控件模板,用自定义的Border作为容器
  • 模板中的Border通过TemplateBinding绑定到ListViewItem的Background、BorderBrush等属性,让Style里的设置能生效
  • 将选中状态的Trigger移到ControlTemplate.Triggers中,直接修改容器Border的背景和边框,避免被默认状态覆盖
  • 保留了你原来设置的CornerRadius、布局相关属性

这样修改后,你设置的默认背景、选中时的红色背景都能正常生效,不会再显示默认的蓝色选中背景。

内容的提问来源于stack exchange,提问作者MaxB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 02:45:44