2016-02-28 85 views
2

Hy there, 我试图启用在2个GridViews之间拖动&,我设法使用“DataPackage”类的自定义类型(SetText,SetBitmap等) )但我无法弄清楚如何用自定义的类/类型来做到这一点。 两个GridView的数据绑定到同一个自定义类(仅几个属性,整型,字符串,BitmapImage的),我只是想直接在此数据项从一个GridView控件拖动到另一个。 非常感谢您的帮助!UWP拖放自定义类型

回答

1

我有同样的问题,请检查这个例子中,我使用的行为,因为我使用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; 

    } 
} 

此行为附加在ListView项目的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模式,只需检查行为的事件。

+1

谢谢里卡多,你的答案我指出了正确的方向: “e.Data.Properties.Add”让我加我的自定义类型的属性,并使用“e.Data.Properties.TryGetValue( )“你得到一个对象,然后你可以投射到你的自定义类型。 工作得很好! – frenchfaso

+0

而这帮助我,谢谢!唯一的区别就是我需要使用e.DataView.Properties.TryGetValue()... – Arwin

3

所以总结为他人谋取利益,我添加了这些事件处理程序,以DataTemplate的内容,我只是想一定(视图模型)的项目类型为可拖动。

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); 
     } 
    } 
+0

这是有帮助的,你知道如何找出插入点?我的GridView是CanReorderItems所以它是一个空间下降,但下降期间()我只是设法从一个的ObservableCollection取出并添加到另一个。 (我有三个独立的GridView,但我可以尝试一个与组....) –