编程语言
首页 > 编程语言> > C#-UWP拖放自定义类型/类

C#-UWP拖放自定义类型/类

作者:互联网

嗨,
我正在尝试启用拖放功能在2个GridView之间移动时,我设法使用“ DataPackage”类的自定义类型(SetText,SetBitmap等)来完成此操作,但是我不知道如何使用自定义类/类型来做到这一点.
两个GridView都将数据绑定到同一个自定义类(只有几个属性,即int,string,bitmapimage),我只是想直接将此数据项从一个GridView拖到另一个.
非常感谢您的帮助!

解决方法:

我遇到了同样的问题,请检查使用行为的示例,因为我使用了MVVM模式,但我对ListView进行了此操作,但对GridView进行了小的改动.

更改行为< ListView>到< GridView>

此行为附加在要拖动项目的ListView中

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;

    }
}

此行为附加在要放置项目的ListView中
这里是另一个行为,以捕获放置事件.

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行为的事件.

标签:win-universal-app,drag-and-drop,c
来源: https://codeday.me/bug/20191027/1943341.html