【问题标题】:Reorder items in ListView using drag and drop使用拖放重新排序 ListView 中的项目
【发布时间】:2014-04-20 00:07:09
【问题描述】:

我尝试使用拖放手势重新排列 ListView 中的项目。

In the "Drop" method I don't know how to get a reference to the "dropped" element, I only get reference to the "target drop" element.

见下文:

private void Grid_Drop(object sender, DragEventArgs e)
        {
            ReorderItem draggedElement = (e.OriginalSource as Grid).DataContext as ReorderItem;
            ReorderItem targetElement = ((Grid)sender).DataContext as ReorderItem;

            Debug.WriteLine("Dragged element is:" + draggedElement.Index);
            Debug.WriteLine("Drag target element is:" + targetElement.Index);
        }

重新排序介于 0 和 1 个索引之间。控制台索引都是 1 :(

<ListView ItemsSource="{Binding Items}" CanReorderItems="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="{Binding Color}"
                              HorizontalAlignment="Stretch"
                              VerticalAlignment="Stretch"
                              AllowDrop="True"
                              Drop="Grid_Drop">
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>

【问题讨论】:

    标签: uwp windows-store-apps


    【解决方案1】:

    既然有人已经完成了,为什么还要重新发明轮子。退房 https://github.com/punker76/gong-wpf-dragdrop。它也可以作为 NuGet 包提供。

    虽然文档使用ListBox,但我使用ListView

    【讨论】:

      【解决方案2】:

      这就是DragEventArgs.Data 的用途。在DragItemsStarting 事件中创建被拖动项目的DataPackageDataPackage 在两个事件之间传递。

      编辑:

      这可以在两个ListViews 之间拖动。根据文档here

      “要使用户能够使用拖放交互对项目重新排序,您必须将 CanReorderItemsAllowDrop 属性都设置为 true。”

      这应该可以解决您的问题。

      【讨论】:

      • &lt;ListView ItemsSource="{Binding Items}" CanReorderItems="True" DragItemsStarting="MyListView_DragItemsStarting"&gt;
      • 是的,我想通了,但现在的问题是在“MyListView_DragItemsStarting”之前调用了“Grid_Drop”...
      • drop 事件是在 ListView.ItemContainer 内的网格上设置的。可能是这个问题吗?也许我必须在 ListView.ItemContainerStyle 上设置 drop 事件,但不知道如何...
      • 啊,注意到here 的一件事是“要使用户能够使用拖放交互重新排序项目,您必须将CanReorderItemsAllowDrop 属性都设置为true。”试试看。
      • ListView 中没有CanReorderItems 属性:/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多