WinUI3 ListView 获取项目被放下的位置

问题描述 投票:0回答:1

有了 ListView,如何获取列表中同一列表中的项目被删除的索引?(重新排序)我尝试使用以下回调,但

DragItemsCompletedEventArgs
OnDropIntoStagedPhotos
似乎都没有合适的方法。

void TableContentPage::OnStagedDragItemsCompleted(
    [[maybe_unused]] ::winrt::Windows::Foundation::IInspectable const &,
    [[maybe_unused]] ::winrt::Microsoft::UI::Xaml::Controls::
        DragItemsCompletedEventArgs const &args)
{

}

void TableContentPage::OnDropIntoStagedPhotos(
    [[maybe_unused]] Windows::Foundation::IInspectable const  &sender,
    [[maybe_unused]] Microsoft::UI::Xaml::DragEventArgs const &args)
{
}

我正在使用以下 ListView 属性:

<ListView x:Name="ListViewName"
          Grid.Row="0"
          Grid.Column="0"
          ScrollViewer.HorizontalScrollMode="Enabled"
          ScrollViewer.HorizontalScrollBarVisibility="Visible"
          ScrollViewer.IsHorizontalRailEnabled="True"
          CanDragItems="True"
          CanReorderItems="True"
          IsSwipeEnabled="True"
          HorizontalAlignment="Stretch"
          AllowDrop="True"
          Background="{StaticResource PrimaryColor}"
          Drop="OnDropIntoStagedPhotos"
          DragOver="OnDragOverStagedPhotos"
          DragItemsCompleted="OnStagedDragItemsCompleted"
          DragItemsStarting="OnStagedDragItemsStarting"
          SelectionMode="Extended"
          SelectionChanged="OnStagedPhotosSelectionChanged">
windows uwp winui-3
1个回答
0
投票

ListViewBase.DragItemsCompleted 给出拖动的项目,使用 IVector.IndexOf,您可以检索索引。所以,我们只需要区分重新排序和移动即可。目的地要给差价是有必要的

有一个 UWP XamlDragAndDrop 示例可以参考。

/// <summary>
/// Drop on the Trash
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Scenario1_ListView::TargetTextBlock_Drop(Platform::Object^ sender, Windows::UI::Xaml::DragEventArgs^ e)
{
    if (e->DataView->Contains(StandardDataFormats::Text))
    {
        // We need to take the deferral as the source will read _deletedItem which
        // we cannot set synchronously
        auto def = e->GetDeferral();
        create_task(e->DataView->GetTextAsync()).then([this, e, def](String^ s)
        {
            _deletedItem = s;
            e->AcceptedOperation = DataPackageOperation::Move;
            def->Complete();
        });
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.