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