【问题标题】:Why do I not get DragItemsCompleted and DragItemsStarting events when CanReorderItems is set to true on a UWP ListView?当 UWP ListView 上的 CanReorderItems 设置为 true 时,为什么我没有得到 DragItemsCompleted 和 DragItemsStarting 事件?
【发布时间】:2021-02-11 06:21:54
【问题描述】:

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

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

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

这些事件让我可以原子地处理拖动,而不是依赖来自ItemsSource 的两个CollectionChanged 事件。

如何让这些事件触发?

【问题讨论】:

    标签: xaml listview uwp windows-runtime


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2021-06-29
      • 2016-06-13
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多