首先将ListView的HideSelection属性设置为True。或者在程序入口函数初始化ListView,加入如下初始化函数
private void initBurnUIDListView()
{
g_loadUIDlistView.Items.Clear();
g_loadUIDlistView.HideSelection = true;
this.g_loadUIDlistView.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(this.g_loadUIDlistView_ItemSelectionChanged);
}
private int iOld = -1;
private void g_loadUIDlistView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (g_loadUIDlistView.SelectedIndices.Count > 0) //若有选中项
{
if (iOld == -1)
{
g_loadUIDlistView.Items[g_loadUIDlistView.SelectedIndices[0]].BackColor = SystemColors.Highlight; //设置选中项的背景颜色
iOld = g_loadUIDlistView.SelectedIndices[0]; //设置当前选中项索引
}
else
{
if (g_loadUIDlistView.SelectedIndices[0] != iOld)
{
g_loadUIDlistView.Items[g_loadUIDlistView.SelectedIndices[0]].BackColor = SystemColors.Highlight; //设置选中项的背景颜色
g_loadUIDlistView.Items[iOld].BackColor = SystemColors.Window;//恢复默认背景色
iOld = g_loadUIDlistView.SelectedIndices[0]; //设置当前选中项索引
}
}
}
else //若无选中项
{
g_loadUIDlistView.Items[iOld].BackColor = SystemColors.Window;//恢复默认背景色
iOld = -1; //设置当前处于无选中项状态
}
}