您可以自己绘制选定的ListViewItem.ListViewSubItem,所有者绘制 Control (set ListView.OwnerDraw = true),然后处理该ListView.DrawSubItem事件。
\nListView.DrawColumnHeader事件可以使用默认值。
\xe2\x96\xb6 我正在使用TextRenderer,因为这是默认渲染器。如果您使用Graphics.DrawText,您会注意到其中的差异。
TextFormatFlags flags = TextFormatFlags.LeftAndRightPadding |\n TextFormatFlags.VerticalCenter;\n\nprivate void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)\n{\n var lv = sender as ListView;\n var subItem = lv.HitTest(lv.PointToClient(MousePosition)).SubItem;\n\n if (subItem != null && e.SubItem == subItem) {\n using (var brush = new SolidBrush(SystemColors.Highlight)) {\n e.Graphics.FillRectangle(brush, e.SubItem.Bounds);\n }\n TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, \n e.Bounds, SystemColors.HighlightText, flags);\n }\n else {\n e.DrawDefault = true;\n }\n}\n\nprivate void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) \n => e.DrawDefault = true;\n\n// Invalidate on a mouse interaction, otherwise the ListView doesn\'t redraw the SubItem\nprivate void listView1_MouseUp(object sender, MouseEventArgs e)\n => (sender as ListView).Invalidate();\nRun Code Online (Sandbox Code Playgroud)\n或者,您可以在通知鼠标交互时更改子项的颜色(此处,使用MouseDown事件)并保存先前的状态(此处仅保存颜色)。最好保存状态,因为每个 SubItem 都可以有自己的设置,因此您不能只恢复到父 ListViewItem 或 ListView 值。
如前所述,UseItemStyleForSubItems = false在每个父 ListViewItem 中进行设置,否则颜色设置将被忽略。
\n此外,FullRowSelect必须设置为false,否则就没有意义:)
这里,状态保存在一个名为 Tuple Field 的可为空的字段中(ListViewSubItem, Color[])。
\n类对象可能更好,只是更短。
private (ListViewItem.ListViewSubItem Item, Color[] colors)? previousItem = null;\n\nprivate void listView1_MouseDown(object sender, MouseEventArgs e)\n{\n var lv = sender as ListView;\n var subItem = lv.HitTest(e.Location).SubItem;\n\n if (previousItem.HasValue) {\n // If an Item\'s Colors have been changed, restore the state\n // It removes the selection if you click in an empty area\n previousItem.Value.Item.BackColor = previousItem.Value.colors[0];\n previousItem.Value.Item.ForeColor = previousItem.Value.colors[1];\n lv.Invalidate(previousItem.Value.Item.Bounds);\n }\n\n if (subItem != null) {\n // Save the SubItem\'s colors state\n previousItem = (subItem, new[] { subItem.BackColor, subItem.ForeColor });\n // Set new Colors. Here, using the default highlight colors\n subItem.BackColor = SystemColors.Highlight;\n subItem.ForeColor = SystemColors.HighlightText;\n lv.Invalidate(subItem.Bounds);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n这就是这个东西的工作原理:
\n\n\xe2\x96\xb6 关于项目/子项目索引,正如问题中提到的:
\n当您使用ListView.HitTestListViewItem检索/单击时SubItem
var hitTest = lv.HitTest(e.Location);\nRun Code Online (Sandbox Code Playgroud)\n那么ListViewItem索引当然是:
\nvar itemIndex = hitTest.Item.Index;\nRun Code Online (Sandbox Code Playgroud)\n是SubItem.Index:
var subItemIndex = hitTest.Item.SubItems.IndexOf(hitTest.SubItem);\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1660 次 |
| 最近记录: |