UWP拖放自定义类型/类

时间:2016-02-28 09:59:17

标签: c# drag-and-drop win-universal-app

那里, 我试图启用拖放功能在两个GridView之间删除,我设法使用" DataPackage"的自定义类型。 class(SetText,SetBitmap等)但我无法弄清楚如何使用自定义类/类型执行此操作。 两个GridView都绑定到同一个自定义类(只有几个属性,int,string,bitmapimage),我只是想将这些数据项从一个GridView直接拖到另一个。 非常感谢你的帮助!

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

我遇到了同样的问题,请查看这个示例我使用了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模式,只需检查“行为”的事件。