use*_*235 2 c# xaml drag-and-drop uwp
我有以下 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">
Run Code Online (Sandbox Code Playgroud)
并将我的事件处理程序定义为
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");
}
Run Code Online (Sandbox Code Playgroud)
我不知道还能做些什么.我想在同一个列表中实现列表项的移动.
我去了正式的Windows 10样本,寻找拖放样本并将其修剪下来(就像从目标移除到源的拖动一样).事实证明,您甚至不必处理任何事件以在一件ListView作品中重新订购.
<ListView x:Name="TargetListView"
Grid.Row="2" Grid.Column="1" Margin="8,4"
CanReorderItems="True" CanDrag="True" AllowDrop="True"
/>
Run Code Online (Sandbox Code Playgroud)
检查您的ObservableCollection重新排序项目后,您会发现它是正确的.如果您想跟踪重新订购,您必须检查CollectionChanged您的事件ObservableCollection,因为没有事件ListView可以执行此操作.
如果你想支持多个列表视图的拖放,我想再看一下样本.