下面我尝试移动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);
}
}
}
1条答案
按热度按时间nkhmeac61#
尽管ListViewItem.Group.Items.Insert()方法按预期工作(Item插入到指定位置),但Group本身并不根据分配给新Item的Index(Group Index)对其项目进行排序。
项目的现有顺序优先,因此最后添加新项目。
如果在更改项的顺序时根据自己的条件为ListView分配自定义ListViewItemSorter,则可以重新定义此行为。
你可以删除
currentViewItem
引用,这不是更有必要。请参见代码。DragOver
事件处理程序也不是必需的,但它最终可用于显示InsertionMark。保持ListView的初始化,更改处理程序的名称:
替换事件处理程序中的代码:
自定义
ListViewItemSorter
对象:此自定义对象可以基于SubItem的
Text
属性(默认)、Items的Index或Group Index对ListView进行排序。它是这样工作的: