惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
J
Java Code Geeks
博客园_首页
Scott Helme
Scott Helme
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
V
Visual Studio Blog
Cloudbric
Cloudbric
Jina AI
Jina AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
W
WeLiveSecurity
K
Kaspersky official blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
量子位
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - wuty007

C# 范围运算符 C# 调用WGC 实现桌面屏幕的捕获 完善基于WPF开发的标尺控件(含实例代码) C# 依赖注入 Microsoft.Extensions.DependencyInjection 实现 控制反转(IOC) C# 获取Windows系统的设备名称 记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题 WPF 调用 Win32的SetWindowDisplayAffinity 函数 实现捕获屏幕时,过滤指定的窗口 记录WPF 在清单列表设置了UIACESS为true,没有签名的报错“从服务器返回了一个参照” WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离 记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题 记录 使用PsExec启动System权限的WPF 程序 记录 命令行的 findstr 的使用 排查Windows 下的内存使用率过高,但是任务管理器看不到进程 Everything 支持 多实例 运行 指定应用 在 控制面板或者 设置的安装应用 置灰或隐藏卸载按钮 WPF 通过RawInput 获取 系统全局触摸事件 C# 定时任务 Quartz.NET 的使用 记录一下Windows系统下的命令行参数的字符个数限制 WPF 实现支持动态调整高度的文本显示控件
WPF 的ListBox 去除默认的Item项的 鼠标hover的背景颜色
wuty007 · 2025-11-05 · via 博客园 - wuty007

一、发现的问题

1、最近在做一个新的桌面应用。里边的UI好多使用到了ListBox的。如下图所示

image

 2、使用的Xmal的样式如下:

    <!--设备列表-->
    <Border Grid.Row="0" Grid.Column="1">
        <ListBox ItemsSource="{Binding DeviceUserInfos}" Style="{StaticResource Style.ListBox.Simple}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
            <!--重写ListBox的控件模板-->
            <ListBox.Template>
                <ControlTemplate>
                    <Grid>
                        <ScrollViewer Style="{StaticResource Style.ScrollViewer.Default}" ManipulationBoundaryFeedback="UIElement_OnManipulationBoundaryFeedback">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Grid>
                </ControlTemplate>
            </ListBox.Template>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <controls:DeviceUserItem Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=ListBox},Converter={StaticResource ListBoxWidthConverter}}" Height="100" DeviceUserStatus="{Binding DeviceUserStatus}" DeviceName="{Binding DeviceName}" DeviceUserColor="{Binding DeviceUserColor}"/>
                </DataTemplate>
           </ListBox.ItemTemplate>
        </ListBox>
    </Border>

在上图会发现,鼠标移动上去了之后,选中的Item项会有浅蓝色的默认背景,这个显然跟UI给出的需求明显不符合。

二、解决问题:

1、增加对ListBoxItem的ItemContainerStyles的控件模板的重写

 <Style x:Key="ItemContainerStyles" TargetType="ListBoxItem">
     <Setter Property="SnapsToDevicePixels" Value="true"/>
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="ListBoxItem">
                 <Border Name="Border"
                         Padding="7" 
                         Background="Transparent" 
                         SnapsToDevicePixels="True">
                     <ContentPresenter />
                 </Border>
                 <ControlTemplate.Triggers>
                     <Trigger Property="IsEnabled" Value="false">
                         <Setter Property="Foreground" Value="LightGray"/>
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>

2、ListBox的ItemContainerStyles 引用 ListBoxItem的样式:

ItemContainerStyle="{StaticResource ItemContainerStyles}"
  <!--设备列表-->
  <Border Grid.Row="0" Grid.Column="1">
      <ListBox ItemsSource="{Binding DeviceUserInfos}" Style="{StaticResource Style.ListBox.Simple}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
ScrollViewer.VerticalScrollBarVisibility
="Visible"
ItemContainerStyle
="{StaticResource ItemContainerStyles}"> <!--重写ListBox的控件模板--> <ListBox.Template> <ControlTemplate> <Grid> <ScrollViewer Style="{StaticResource Style.ScrollViewer.Default}" ManipulationBoundaryFeedback="UIElement_OnManipulationBoundaryFeedback"> <ItemsPresenter /> </ScrollViewer> </Grid> </ControlTemplate> </ListBox.Template> <ListBox.ItemTemplate> <DataTemplate> <controls:DeviceUserItem Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=ListBox},Converter={StaticResource ListBoxWidthConverter}}" Height="100" DeviceUserStatus="{Binding DeviceUserStatus}" DeviceName="{Binding DeviceName}" DeviceUserColor="{Binding DeviceUserColor}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Border>

3、添加完ListBoxItem 样式如下:背景颜色跟UI相符

image