WPF C#:通过拖放重新排列列表框中的项目 [英] WPF C#: Rearrange items in listbox via drag and drop

查看:36
本文介绍了WPF C#:通过拖放重新排列列表框中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何通过鼠标拖动上下移动预先填充的列表框中的项目.

I am trying to figure out how to move the items in a pre-populated listbox up and down via mouse drags.

我已经查看了 microsoft api 中的 Control.DoDragDrop 方法,但我仍然无法让它做任何事情.

I have looked at the Control.DoDragDrop method from microsoft's api, but I still can't get it to do anything.

如果我是 Visual Studios 环境的新手,我将不胜感激.

I would appreciate any help since I am new to the visual studios environment.

推荐答案

我试过用 observablecollection 创建一个,看看

i've tried create one using observablecollection, have a look

    ObservableCollection<Emp> _empList = new ObservableCollection<Emp>();

    public Window1()
    {
        InitializeComponent();

        _empList .Add(new Emp("1", 22));
        _empList .Add(new Emp("2", 18));
        _empList .Add(new Emp("3", 29));
        _empList .Add(new Emp("4", 9));
        _empList .Add(new Emp("5", 29));
        _empList .Add(new Emp("6", 9));
        listbox1.DisplayMemberPath = "Name";
        listbox1.ItemsSource = _empList;

        Style itemContainerStyle = new Style(typeof(ListBoxItem));
        itemContainerStyle.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true));
        itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(s_PreviewMouseLeftButtonDown)));
        itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.DropEvent, new DragEventHandler(listbox1_Drop)));
        listbox1.ItemContainerStyle = itemContainerStyle;
    }

拖放过程

    void s_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {

        if (sender is ListBoxItem)
        {
            ListBoxItem draggedItem = sender as ListBoxItem;
            DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
            draggedItem.IsSelected = true;
        }
    }

    void listbox1_Drop(object sender, DragEventArgs e)
    {
        Emp droppedData = e.Data.GetData(typeof(Emp)) as Emp;
        Emp target = ((ListBoxItem)(sender)).DataContext as Emp;

        int removedIdx = listbox1.Items.IndexOf(droppedData);
        int targetIdx = listbox1.Items.IndexOf(target);

        if (removedIdx < targetIdx)
        {
            _empList.Insert(targetIdx + 1, droppedData);
            _empList.RemoveAt(removedIdx);
        }
        else
        {
            int remIdx = removedIdx+1;
            if (_empList.Count + 1 > remIdx)
            {
                _empList.Insert(targetIdx, droppedData);
                _empList.RemoveAt(remIdx);
            }
        }
    }

注意:

  • 实现中的一件事很糟糕,因为它使用了 PreviewMouseLeftButtonDown 事件,拖动的项目看起来像没有被选中
  • 而且为了更容易实现,放置目标是列表框项目而不是列表框本身 - 可能需要一个更好的解决方案
  • one thing sucks in the implementation is since it use the PreviewMouseLeftButtonDown event, the dragged item looks like not selected
  • and also for an easier implementation the drop target is the list box items and not the listbox itself - might need a better solution for this

这篇关于WPF C#:通过拖放重新排列列表框中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆