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

如何关闭WPF项目中ListBox的悬停高亮效果?

解决方案

这个问题是Win10版本更新后,WPF默认系统控件样式中新增了ListBoxItem的悬停高亮触发规则导致的,你可以通过重写ListBoxItem的默认样式关闭该效果,以下是两种适用场景的实现方式:

1. 全局生效(项目内所有ListBox统一关闭悬停高亮)

把以下XAML代码添加到你项目的App.xaml的<Application.Resources>节点内即可:

<Style TargetType="ListBoxItem">
    <Style.Resources>
        <!-- 悬停状态背景设为透明,也可替换为你需要的非高亮颜色值 -->
        <SolidColorBrush x:Key="Item.MouseOver.Background" Color="Transparent"/>
        <SolidColorBrush x:Key="Item.MouseOver.Border" Color="Transparent"/>
        <!-- 保留选中状态样式,不需要的话也可修改对应资源值 -->
        <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#0078D7"/>
        <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#0078D7"/>
    </Style.Resources>
</Style>

2. 单个ListBox生效(只针对指定ListBox关闭悬停高亮)

如果不需要全局修改,直接把样式附加到对应ListBox的ItemContainerStyle属性上即可:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="Item.MouseOver.Background" Color="Transparent"/>
                <SolidColorBrush x:Key="Item.MouseOver.Border" Color="Transparent"/>
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <!-- 此处放置ListBox的其他内容 -->
</ListBox>

补充说明

  • 如果你之前的项目已经自定义过ListBoxItem的控件模板,也可以直接在模板的VisualStateManager节点中找到MouseOver状态,删除对应的背景、边框修改逻辑即可。
  • 该方案兼容VB.net 2017对应的WPF版本,不需要修改后台逻辑代码,仅通过XAML配置即可生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 01:45:04