简体   繁体   English

如何强制ListView忽略单击/不突出显示单击的项目?

[英]How can I force a ListView to disregard clicking/not highlight a clicked item?

由于我没有根据用户可能选择的内容处理ListView中的项目(处理总是使用列表中的所有内容),我想禁止选择可能使用户认为他将处理限制为那个项目(我已经有多选=假,所以这不是问题)。

There is not any property to disable item selection in the ListView . ListView没有任何属性可以禁用项目选择。

What you can do is to handle the event that notifies that an item has been selected by attaching an event handler to ItemSelectionChanged and then deselect the item: 您可以做的是通过将事件处理程序附加到ItemSelectionChanged来处理通知已选择项目的事件,然后取消选择该项目:

yourListView.ItemSelectionChanged += yourListView_ItemSelectionChanged;

private void yourListView_ItemSelectionChanged(
    object sender,
    ListViewItemSelectionChangedEventArgs e)
{
   if (e.IsSelected)
      e.Item.Selected = false;
}

To add to Adriano Repettis soloution, I had something similar where I grayed out the items I wanted to block, his solution prevents the blue highlight but the Item still has focus which causes a problem when the item's backcolor is set to anything but white since some of the selected line becomes white. 为了添加到Adriano Repettis soloution,我有类似的东西,我把我想要阻挡的项目变灰,他的解决方案阻止了蓝色突出显示但是项目仍然有焦点,当项目的背景颜色被设置为除了白色以外的任何颜色时会导致问题所选行的颜色变为白色。 to handle this, I suggest adding the line: 为了解决这个问题,我建议添加一行:

e.Item.Focused = false;

the final code: 最终代码:

yourListView.ItemSelectionChanged += yourListView_ItemSelectionChanged;

private void yourListView_ItemSelectionChanged(
    object sender,
    ListViewItemSelectionChangedEventArgs e)
{
    if (e.Item.BackColor == Color.LightGray)
    {
        e.Item.Selected = false;
        e.Item.Focused = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM