如何取消选择ListView中的项目?

Zee*_*mee 10 .net c# compact-framework

我有一个ListView,里面有几个项目.当ListView失去焦点时,最后选择的ListViewItem仍然被"选中",背景为灰色.
我想在ListView.FocusLost上实现它,选择已经消失,因此将发生ListView.SelectedIndexChanged事件.
有任何想法吗?

我使用的是.NET CF 3.5.

Vla*_*mir 19

假设您正在从父窗体/控件访问ListView.

您可以在表单的/ control的构造函数/ load事件中添加这段代码:

this.myListView.LostFocus += (s, e) => this.myListView.SelectedIndices.Clear();
Run Code Online (Sandbox Code Playgroud)

好的,所以在你的情况下,你将用以下代码替换该委托:

if (this.myListView.SelectedIndices.Count > 0)
    for (int i = 0; i < this.myListView.SelectedIndices.Count; i++)
    {
        this.myListView.Items[this.myListView.SelectedIndices[i]].Selected = false;
    }
Run Code Online (Sandbox Code Playgroud)

你可以给代码一个更好的形式,顺便说一句.

  • SelectedIndices 没有 `Clear()` 方法,至少在紧凑型框架 3.5 中没有。 (2认同)

小智 5

myListView.SelectedItems.Clear();
Run Code Online (Sandbox Code Playgroud)