UWP 拖放自定义类型/类

本文关键字:类型 自定义 拖放 UWP | 更新日期: 2023-09-27 18:36:31

Hy 那里,我正在尝试在 2 个 GridView 之间启用拖放,我设法使用"DataPackage"类的自定义类型(SetText、SetBitmap 等)执行此操作,但我无法弄清楚如何使用自定义类/类型执行此操作。两个 GridView 都数据绑定到同一个自定义类(只有几个属性,int、字符串、位图图像),我只想直接将此数据项从一个 GridView 拖到另一个 GridView。非常感谢您的帮助!

UWP 拖放自定义类型/类

因此,为了其他人的利益,我将它们添加到 DataTemplate 内容的事件处理程序中,因为我只希望特定 (ViewModel) 类型的项可拖动。

    private void Grid_Drop(object sender, DragEventArgs e)
    {
        if (sender is FrameworkElement)
        {
            var fe = sender as FrameworkElement;
            var targetIvm = fe.DataContext as ItemViewModel;
            object obj = null;
            if(e.DataView.Properties.TryGetValue("ItemViewModel", out obj))
            {
                var sourceIvm = obj as ItemViewModel;
                vm.MoveItem(sourceIvm, targetIvm);
            }
        }
    }
    private void Grid_DragStarting(Windows.UI.Xaml.UIElement sender, DragStartingEventArgs args)
    {
        if (sender is FrameworkElement)
        {
            var fe = sender as FrameworkElement;
            var item = new KeyValuePair<string, object>("ItemViewModel", fe.DataContext);
            args.Data.RequestedOperation = DataPackageOperation.Move;
            args.Data.Properties.Add("ItemViewModel", fe.DataContext);
        }
    }

我遇到了同样的问题,请检查此示例,我使用了行为,因为我使用了 MVVM 模式,但我为 ListView 执行此操作,但对于 GridView 是相同的,但进行了少量更改。

将"行为"<ListView>更改为<GridView>

此行为附加到要拖动项的列表视图中

public class StartingDragBehavior:Behavior<ListView>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.CanDragItems = true;
        this.AssociatedObject.DragItemsStarting += AssociatedObject_DragItemsStarting;
    }

    private void AssociatedObject_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
    {
        e.Data.RequestedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
        if(e.Items!=null && e.Items.Any())
        {
            e.Data.Properties.Add("item", e.Items.FirstOrDefault());
        }
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.DragItemsStarting -= AssociatedObject_DragItemsStarting;
    }
}

此行为附加到要放置项的列表视图中这是另一个捕获掉落事件的行为。

public class EndDropBehavior : Behavior<ListView>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.AllowDrop = true;
        this.AssociatedObject.Drop += AssociatedObject_Drop;
        this.AssociatedObject.DragOver += AssociatedObject_DragOver;
    }
    private void AssociatedObject_Drop(object sender, Windows.UI.Xaml.DragEventArgs e)
    {
        if (e.DataView != null &&
            e.DataView.Properties != null &&
            e.DataView.Properties.Any(x => x.Key == "item" && x.Value.GetType() == typeof(MyObject)))
        {
            try
            {
                var def = e.GetDeferral();
                var item = e.Data.Properties.FirstOrDefault(x => x.Key == "item");
                var card = item.Value as MyObject;

                    var list = sender as ListView;
                    var vm = list.DataContext as Infrastructure.ViewModels.CreditCardsViewModel;

                        vm.MyCollection.Add(card);
                def.Complete();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
        else
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.None;
        }
    }
    private void AssociatedObject_DragOver(object sender, Windows.UI.Xaml.DragEventArgs e)
    {
        if (e.DataView != null &&
            e.DataView.Properties != null &&
            e.DataView.Properties.Any(x => x.Key == "item" && x.Value.GetType() == typeof(MyObject)))
        {
            e.AcceptedOperation = e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
        }
        else
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.None;
        }
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.Drop -= AssociatedObject_Drop;
        this.AssociatedObject.DragOver -= AssociatedObject_DragOver;
    }
}

如果您没有使用 MVVM 模式,只需检查 to 行为的事件。