首页 > 解决方案 > 当 UWP ListView 上的 CanReorderItems 设置为 true 时,为什么我没有得到 DragItemsCompleted 和 DragItemsStarting 事件?

问题描述

我有一个 UWP XAML <ListView>,我希望用户通过拖放重新排列。ListViewBase 上的CanReorderItems属性提供了该功能,并且只需要几个属性:

<ListView ItemsSource="{x:Bind Items}"
    CanReorderItems="True"
    AllowDrop="True"
    DragItemsStarting="ListView_DragItemsStarting"
    DragItemsCompleted="ListView_DragItemsCompleted">
    <!-- ... -->
</ListView>

这成功地让我在 ListView 中拖放项目并在(删除然后添加)上触发CollectionChanged事件。ItemsSource但是,它不会触发DragItemsStartingandDragItemsCompleted事件。

CollectionChanged这些事件让我可以原子地处理拖动,而不是依赖ItemsSource.

如何让这些事件触发?

标签: xamllistviewuwpwindows-runtime

解决方案


ListViews 不会触发DragItemsStartingDragItemsCompleted事件,除非CanDragItems在 ListView 上设置为 true:

<ListView ItemsSource="{x:Bind Items}"
    CanDragItems="True"
    CanReorderItems="True"
    AllowDrop="True"
    DragItemsStarting="ListView_DragItemsStarting"
    DragItemsCompleted="ListView_DragItemsCompleted">
    <!-- ... -->
</ListView>

添加此属性后,您应该会发现这些事件将触发。


推荐阅读