c# - 如何在winfoms中更改列表视图的选定行背景
问题描述
如果行中的数字为 0,我想更改所选行的背景。我尝试关注但对我不起作用。
list.SelectedItems[0].BackColor = Color.Red;
解决方案
据我所知,ListView使用windows的默认设计颜色作为选择颜色,所以如果你想改变它,你必须自己绘制项目。
您可以通过将 ListView 的“OwnerDraw”属性设置为 true 并为 ListView 的“DrawItem”事件编写自己的逻辑来做到这一点。
所以是这样的:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
if (e.Item.Selected)
{
if (e.Item.Index == 0)
{
e.Graphics.FillRectangle(Brushes.Red, e.Bounds);
}
else
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
}
}
else
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
}
e.DrawText(TextFormatFlags.TextBoxControl);
}
推荐阅读
- nginx - 将 RTSP 转换为 HLS Nginx 以流式传输时出现问题
- ms-access - 使用表单将发票从 Access 导出到 QuickBooks 时遇到问题
- r - 为什么摘要在几百列之后不显示结果?
- python - 为什么我的代码在每次运行后从相同图像中的相同列表中查找更多对象?
- reactjs - TypeError:无法读取未定义的属性“图像” - React & Gatsby
- typescript - 引入两个别名的模拟导入
- flutter - 如何在颤动中对多个小部件使用相同的键?
- php - 如何在 WooCommerce 中获取用户的活动订单 ID 和订单状态
- reactjs - 如何使用参数调用 React Context 函数
- java - 如何将文件夹构建到 spring boot gradle 项目的 .jar 根目录中?