int selectedRowIndex = 0;
private void listViewUserList_SelectedIndexChanged(object sender, EventArgs e)
{
if(listViewUserList.SelectedIndices.Count == 0)
return;
}
private void Form_Load(object sender, EventArgs e)
{
if(listViewUserList.Items.Count > 0)
{
listViewUserList.Items[selectedRowIndex].Selected = true;
listViewUserList.Select();
}
}
在表单加载中,我在ListView
中选择第一行。但是当我选择空行时,它会取消选择ListView
中的项目。
点击空行然后选择之前选择的行,我该怎么办?我在Google上搜索过但没有找到任何好的答案。仅针对WPF而不是针对Windows窗体的stackoverflow的答案。
答案 0 :(得分:0)
myListView.Select()
; <强>代码:强>
private void Form_Load(object sender, EventArgs e)
{
if(listViewUserList.Items.Count > 0 && (String.Empty != listViewUserList.Items[selectedRowIndex].ToString() ))
{
listViewUserList.Items[selectedRowIndex].Selected = true;
listViewUserList.Select();
}
}
答案 1 :(得分:0)
这可以通过为ListView
的{{1}}事件添加事件处理程序来完成。如果您仍需要MouseUp
的事件处理程序及其委托方法SelectedIndexChanged
,请从中删除处理项目选择的任何代码:
listViewUserList_SelectedIndexChanged
添加以下事件处理程序:
private void listViewUserList_SelectedIndexChanged(object sender, EventArgs e)
{
// remove the 2 lines below, as the selection handling is done in the
// MouseUp event handler
//if(listViewUserList.SelectedIndices.Count == 0)
// return;
// TODO: get the column data
}
和以下方法:
listViewUserList.MouseUp += new MouseEventHandler(this.listViewUserList_MouseUp);