【发布时间】:2016-04-29 13:26:41
【问题描述】:
我有一个带有 DragDrop 功能的 ListView。我希望拖动的项目在 DragDrop 之后保持选中状态。
我有这个代码(用于 DragDrop)
private void commandListView_DragDrop(object sender, DragEventArgs e)
{
Point point = commandListView.PointToClient(new Point(e.X, e.Y));
int index = 0;
try
{
index = commandListView.GetItemAt(point.X, point.Y).Index;
}
catch (Exception)
{
}
if (index < 0)
{
index = commandListView.Items.Count - 1;
}
ListViewItem data = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
commandListView.Items.Remove(data);
commandListView.Items.Insert(index, data);
}
我试图用这个再次选择项目,但它不起作用
data.Selected = true;
data.Focused = true;
然后我测试了我是否可以专注于 ListView 中的第一项
commandListView.Items[0].Selected = true;
commandListView.Items[0].Focused = true;
但它也不起作用,所选项目没有改变。它始终是拖动项目在拖放之前的旧索引。
PS。我正在使用 WinForms
@Update
我已经试过了
commandListView.Focus();
但是没用
只是为了澄清拖放发生在同一个 ListView 中,我正在拖动项目以更改它们的顺序。
【问题讨论】:
-
旁注:永远不要使用
catch (Exception) {}(忽略所有抛出的异常) -
我不确定,但也许这可以帮助你:stackoverflow.com/questions/11745028/…
-
commandListView.Focus();你必须在焦点项目之后对控件本身进行焦点。 -
@DmitryBychenko 是的,我知道,我将其用作个人工具,我最不想要的就是例外哈哈哈。