问题描述
我有以下 ListView
<ListView Name="listAccounts" Width="300" AllowDrop="True" SelectionMode="Single" CanDragItems="True" CanDrag="True" CanReorderItems="True" Background="{ThemeResource myBackground}" DragItemsStarting="listAccounts_DragItemsStarting" Drop="listAccounts_Drop">
并将我的事件处理程序定义为
and defined my event handlers as
private void listAccounts_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
e.Data.SetData("itemIndex", (e.Items[0] as AccountList).Text.ToString());
}
private async void listAccounts_Drop(object sender, DragEventArgs e)
{
string itemIndexString = await e.Data.GetView().GetTextAsync("itemIndex");
}
我不知道还能做些什么。我想在同一个列表中实现列表项的移动。
I don't know what else I can do. I want to realize the movement of the list items in the same list.
推荐答案
我转到官方的Windows 10样本,查找并将其修剪掉(例如从目标到源代码的删除)。原来你甚至不需要处理任何事件,以便在单个 ListView
工作中重新排序。
I went over to the official Windows 10 samples, looked for the drag-drop sample and trimmed it down (like removing the drag from target to source). Turns out you don't even have to handle any events to make re-ordering in a single ListView
work.
<ListView x:Name="TargetListView"
Grid.Row="2" Grid.Column="1" Margin="8,4"
CanReorderItems="True" CanDrag="True" AllowDrop="True"
/>
在重新排序项目后,检查您的 ObservableCollection
我们会注意到这是正确的。如果要跟踪重新排序,您必须在 ObservableCollection
上查看 CollectionChanged
事件 ListView
没有事件执行此操作。
Check your ObservableCollection
after reordering items and you'll notice it's correct. If you want to track the re-ordering, you'll have to check the CollectionChanged
event on your ObservableCollection
, as there is no event on the ListView
to do this.
如果您要支持拖放放弃多个列表浏览,我想再看一下样本。
If you want to support drag & drop accross multilple listviews, I'd say have another look at the sample.
这篇关于在同一ListView中拖放项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!