【问题标题】:Drag and drop item in same ListView在同一个 ListView 中拖放项目
【发布时间】:2016-04-02 12:41:02
【问题描述】:

我有关注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">

并将我的事件处理程序定义为

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");
}

我不知道我还能做什么。我想实现列表项在同一个列表中的移动。

【问题讨论】:

    标签: c# xaml drag-and-drop uwp


    【解决方案1】:

    我查看了官方的 Windows 10 示例,查找了 drag-drop sample 并将其修整(例如移除从目标到源的拖动)。事实证明,您甚至不必处理任何事件即可在单个 ListView 中进行重新排序。

    <ListView x:Name="TargetListView"
                    Grid.Row="2" Grid.Column="1" Margin="8,4"
                    CanReorderItems="True" CanDrag="True" AllowDrop="True"
                    />
    

    在重新订购商品后检查您的ObservableCollection,您会发现它是正确的。如果您想跟踪重新排序,则必须检查 ObservableCollection 上的 CollectionChanged 事件,因为 ListView 上没有事件可以执行此操作。

    如果您想支持跨多个列表视图的拖放,我会说再看一下示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2013-01-05
      • 2014-07-11
      • 1970-01-01
      相关资源
      最近更新 更多