2016-02-28 106 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,但我可以嘗試一個與組....) –