我有一个带有ListView函数的DragDrop。我希望拖动的项目保持选中后的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中正在发生的拖放,我正在拖动项目以更改它们的顺序。
发布于 2016-04-29 21:57:09
我找到了一个解决方案;我使用MouseDown事件来启动DragDrop操作。
现在我使用ItemDrag事件,一切都很好,实际上我甚至不需要聚焦项目,它是自动完成的。
发布于 2022-11-27 08:23:16
对于那些仍在寻找解决方案的人来说:
int counter = ...; // your code to find index of wanted ListView-Item.
ListVieuwName.Focus(); //First: activate focus on the entire ListView.
ListVieuwName.Items[counter].Selected = true; //Next: select your wanted ListView-Item.
这应该行..。
https://stackoverflow.com/questions/36939616
复制