使用拖放重新排序 ListView 中的项目

Ale*_*cus 3 windows-store-apps uwp

我尝试使用拖放手势对 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);
        }
Run Code Online (Sandbox Code Playgroud)

重新排序介于 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>
Run Code Online (Sandbox Code Playgroud)

Zeu*_*s82 5

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

尽管文档使用了ListBox,但我将它与ListView