winforms 如何移动一个组的ListViewItem部分来代替另一个使用相同ListView的ListViewItem?

plupiseo  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(128)

下面我尝试移动item4来代替item5,我预期的操作是项目4位于item5之上,项目5位于item4之下:

下面我尝试移动item4来代替item5,我预期的操作是item4位于item5之上,item5位于item4之下:

我也不能移动一个项目的地方另一个,谁能帮助?
我在这里留下完整的代码,包括设计:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        private ListView listView;
        private ListViewItem currentVieItem;
        public Form1()
        {
            InitializeComponent();
            
            createListview();
        }

        public void createListview()
        {
            listView = new ListView();
            listView.AllowDrop = true;

            listView.ItemDrag += new ItemDragEventHandler(OnItemDrag);
            listView.DragOver += new DragEventHandler(OnDragOver);
            listView.DragDrop += new DragEventHandler(OnDragDrop);

            // MY CODE HERE
            ColumnHeader columnHeader = new ColumnHeader();
            listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            columnHeader});
            listView.HideSelection = false;
            int indexGroup = 1;
            ListViewGroup group = new ListViewGroup();
            for (int i = 0; i < 100; i++)
            {
                if (i % 5 == 0)
                {
                    string nmGroup = $"Group {indexGroup}";
                    group = new ListViewGroup() { Header = nmGroup};
                    listView.Groups.Add(group);
                    indexGroup++;
                }
                listView.Items.Add(new ListViewItem() { Text = $"Item {i}", Group = group });
            }
            listView.Location = new System.Drawing.Point(12, 12);
            listView.Name = "listView1";
            listView.Size = new System.Drawing.Size(436, 494);
            listView.TabIndex = 0;
            listView.UseCompatibleStateImageBehavior = false;
            listView.View = System.Windows.Forms.View.Details;
            // 
            // columnHeader1
            // 
            columnHeader.Text = "Items";
            columnHeader.Width = 382;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(937, 600);
            this.Controls.Add(listView);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        public void OnDragOver(object sender, DragEventArgs e)
        {
            var pos = listView.PointToClient(new Point(e.X, e.Y));
            var hit = listView.HitTest(pos);
            this.currentVieItem = hit.Item;
            this.Text = hit.Item?.Index.ToString();

            if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection)))
            {
                e.Effect = e.AllowedEffect;
            }
        }

        public void OnDragDrop(object sender, DragEventArgs e)
        {
            if (currentVieItem == null) return;

            int index = currentVieItem.Index;
            this.Text = index.ToString();

            if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection)))
            {
                if (e.Effect == DragDropEffects.Move)
                {
                    foreach (ListViewItem current in (ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)))
                    {
                        current.Remove();
                        current.Group = currentVieItem.Group;
                        listView.Items.Insert(index, current);
                        index++;
                    }
                }                   
            }
        }

        public void OnItemDrag(object sender, ItemDragEventArgs e)
        {
            listView.DoDragDrop(listView.SelectedItems, DragDropEffects.Move);
        }
    }
}
nkhmeac6

nkhmeac61#

尽管ListViewItem.Group.Items.Insert()方法按预期工作(Item插入到指定位置),但Group本身并不根据分配给新Item的Index(Group Index)对其项目进行排序。
项目的现有顺序优先,因此最后添加新项目。
如果在更改项的顺序时根据自己的条件为ListView分配自定义ListViewItemSorter,则可以重新定义此行为。
你可以删除currentViewItem引用,这不是更有必要。请参见代码。
DragOver事件处理程序也不是必需的,但它最终可用于显示InsertionMark
保持ListView的初始化,更改处理程序的名称:

// [...]
listView.ItemDrag += OnLVItemDrag;
listView.DragEnter += OnLVDragEnter;
listView.DragDrop += OnLVDragDrop;
// [...]

替换事件处理程序中的代码:

public void OnLVItemDrag(object sender, ItemDragEventArgs e)
{
    var lv = sender as ListView;
    lv.DoDragDrop(lv.SelectedItems, DragDropEffects.Move);
}

private void OnLVDragEnter(object sender, DragEventArgs e) => e.Effect = e.AllowedEffect;

public void OnLVDragDrop(object sender, DragEventArgs e)
{
    var data = e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)) as ListView.SelectedListViewItemCollection;
    if (data is null) return;

    var lv = sender as ListView;
    var nearestItem = lv.HitTest(lv.PointToClient(new Point(e.X, e.Y))).Item;
    if (nearestItem is null) return;

    var groupIndex = nearestItem.Group.Items.IndexOf(nearestItem);

    if (e.Effect == DragDropEffects.Move) {
        foreach (ListViewItem item in data) {
            // Item dropped over itself or Items dropped over the same selection
            if (nearestItem == item) continue;
            nearestItem.Group.Items.Remove(item);
            nearestItem.Group.Items.Insert(groupIndex, item);
            if (nearestItem.Group != item.Group) groupIndex++;
        }
        listView.Sort();
    }
    // else{} Handle other operations
}

自定义ListViewItemSorter对象

此自定义对象可以基于SubItem的Text属性(默认)、Items的Index或Group Index对ListView进行排序。

using System.Collections;

class ListViewSorter : IComparer {
    int columnIdx = 0;
    bool indexSort = false;
    bool sortGroups = false;

    public ListViewSorter() { }
    public ListViewSorter(int column) => columnIdx = column;

    public ListViewSorter(bool sortByIndex, bool useGroupIndex) { 
        sortGroups = useGroupIndex;
        indexSort = sortByIndex;
    }

    public int Compare(object lvi1, object lvi2)
    {
        var item1 = lvi1 as ListViewItem;
        var item2 = lvi2 as ListViewItem;
        if (indexSort) {
            if (sortGroups && item1.Group != null && item2.Group != null) {
                int idx1 = item1.Group.Items.IndexOf(item1);
                int idx2 = item2.Group.Items.IndexOf(item2);
                return idx1 - idx2;
            }
            else {
                return item1.Index - item2.Index;
            }
        }
        else {
            return string.Compare(
                item1.SubItems[columnIdx].Text, 
                item2.SubItems[columnIdx].Text);
        }
    }
}

它是这样工作的:

相关问题