加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在DragDrop事件期间更改ListView中的焦点项?

发布时间:2020-12-15 22:30:23 所属栏目:百科 来源:网络整理
导读:我有一个带DragDrop函数的ListView.我希望拖动的项目在DragDrop之后保持选中状态. 我有这个代码(对于DragDrop) private void commandListView_DragDrop(object sender,DragEventArgs e){ Point point = commandListView.PointToClient(new Point(e.X,e.Y));
我有一个带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中发生的拖拽,我拖动项目来改变他们的顺序.

解决方法

我找到了解决方案;我正在使用MouseDown事件来启动DragDrop操作.

现在我使用ItemDrag事件,一切正常,实际上我甚至不需要关注项目,它是自动完成的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读