繁体   English   中英

UWP拖放自定义类型/类

[英]UWP drag and drop custom type/class

我在那里,我试图在2个GridView之间进行拖放,我设法用“DataPackage”类的自定义类型(SetText,SetBitmap等)来做,但我无法弄清楚怎么做这与自定义类/类型。 两个GridView都绑定到同一个自定义类(只有几个属性,int,string,bitmapimage),我只是想将这些数据项从一个GridView直接拖到另一个。 非常感谢您的帮助!

因此,为了总结其他人的好处,我将这些添加到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);
        }
    }

我有同样的问题,请检查这个例子我使用了Behaviors,因为我使用了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中您要删除项目这里另一个行为来捕获drop事件。

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模式,只需检查“行为”的事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM