2011-06-28 17 views
6

Yakınlarda DragCount (ItemsControl Drag and Drop) kullanarak ItemsControl'ün nasıl yeniden sıraya konulacağı hakkında bir soru sordum. Yanıt şu an için harika çalıştı (aşağıdaki kod) ama şimdi MVVM'yi uygulamaya çalışıyorum ve mevcut çözüm, görünümdeki öğelere erişim gerektiriyor. MVVM ile çalışmak için herhangi bir fikir nasıl değiştirilir? Ben komutlara bağlamak için ekli özelliklerini yapmayı planlıyorsanız ama gibi hatların kurtulmak için nasıl bilmiyorum: int index = (int)(e.GetPosition(DimsContainer).X/width);Öğeleri Yeniden Sıralama MVVM kullanarak Sürükle ve Bırak ile Denetle

Güncel sürükle ve bırak kodu:

private void DimsContainer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     _isDown = true; 
     _startPoint = e.GetPosition(this.DimsContainer); 
    } 

    private void DimsContainer_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     _isDown = false; 
     _isDragging = false; 

     if (_realDragSource != null) 
     { 
      _realDragSource.ReleaseMouseCapture(); 
     } 
    } 

    private void DimsContainer_PreviewMouseMove(object sender, MouseEventArgs e) 
    { 
     if (_isDown) 
     { 
      if ((_isDragging == false) && 
       ((Math.Abs(e.GetPosition(this.DimsContainer).X - _startPoint.X) > 
        SystemParameters.MinimumHorizontalDragDistance) || 
       (Math.Abs(e.GetPosition(this.DimsContainer).Y - _startPoint.Y) > 
        SystemParameters.MinimumVerticalDragDistance))) 
      { 
       _isDragging = true; 
       _realDragSource = e.Source as UIElement; 
       _realDragSource.CaptureMouse(); 

       double width = ((FrameworkElement)(this.DimsContainer.ItemContainerGenerator.ContainerFromIndex(0))).ActualWidth; 
       int index = (int)(e.GetPosition(DimsContainer).X/width); 
       DragDrop.DoDragDrop(_realDragSource, new DataObject("UIElement", index, true), DragDropEffects.Move); 
      } 
     } 
    } 

    private void DimsContainer_DragEnter(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent("UIElement")) 
     { 
      e.Effects = DragDropEffects.Move; 
     } 
    } 

    private void DimsContainer_Drop(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent("UIElement")) 
     { 
      int sourceIndex = (int)e.Data.GetData("UIElement"); 
      int removedObject = this.indices[sourceIndex]; 
      this.indices.RemoveAt(sourceIndex); 

      UIElement droptarget = e.Source as UIElement; 

      // If a drag/drop is happening, there is definitely 
      // one child in the DimsContainer 
      double width = ((FrameworkElement)(this.DimsContainer.ItemContainerGenerator.ContainerFromIndex(0))).ActualWidth; 
      int index = (int)(e.GetPosition(DimsContainer).X/width); 

      try 
      { 
       this.indices.Insert(index, removedObject); 
      } 
      catch (InvalidOperationException) 
      { 
       // ignore 
      } 

      _isDown = false; 
      _isDragging = false; 
      _realDragSource.ReleaseMouseCapture(); 
     } 
    } 

    public static int RemoveItemFromItemsControl(ItemsControl itemsControl, object itemToRemove) 
    { 
     int indexToBeRemoved = -1; 
     if (itemToRemove != null) 
     { 
      indexToBeRemoved = itemsControl.Items.IndexOf(itemToRemove); 

      if (indexToBeRemoved != -1) 
      { 
       IEnumerable itemsSource = itemsControl.ItemsSource; 
       if (itemsSource == null) 
       { 
        itemsControl.Items.RemoveAt(indexToBeRemoved); 
       } 
       // Is the ItemsSource IList or IList<T>? If so, remove the item from the list. 
       else if (itemsSource is IList) 
       { 
        ((IList)itemsSource).RemoveAt(indexToBeRemoved); 
       } 
       else 
       { 
        Type type = itemsSource.GetType(); 
        Type genericIListType = type.GetInterface("IList`1"); 
        if (genericIListType != null) 
        { 
         type.GetMethod("RemoveAt").Invoke(itemsSource, new object[] { indexToBeRemoved }); 
        } 
       } 
      } 
     } 
     return indexToBeRemoved; 
    } 

    public static void InsertItemInItemsControl(ItemsControl itemsControl, object itemToInsert, int insertionIndex) 
    { 
     if (itemToInsert != null) 
     { 
      IEnumerable itemsSource = itemsControl.ItemsSource; 

      if (itemsSource == null) 
      { 
       itemsControl.Items.Insert(insertionIndex, itemToInsert); 
      } 
      // Is the ItemsSource IList or IList<T>? If so, insert the dragged item in the list. 
      else if (itemsSource is IList) 
      { 
       ((IList)itemsSource).Insert(insertionIndex, itemToInsert); 
      } 
      else 
      { 
       Type type = itemsSource.GetType(); 
       Type genericIListType = type.GetInterface("IList`1"); 
       if (genericIListType != null) 
       { 
        type.GetMethod("Insert").Invoke(itemsSource, new object[] { insertionIndex, itemToInsert }); 
       } 
      } 
     } 
    } 

cevap

6

kullanım sürükle ve bırak işlevi. Örneğin. http://www.codeproject.com/KB/WPF/gong-wpf-dragdrop-ii.aspx

+0

Bu benim soruma cevap vermiyor. Sorun şu ki, SAME kapsayıcısını sürükleyip bırakmam gerekiyor ve ben de bağımlılıktan nasıl kurtulacağımı bilmiyorum (satırdaki gibi: int index = (int) (e.GetPosition (DimsContainer)) .X/genişlik);) – KrisTrip

+0

Yukarıdaki makaleyi kontrol ettiniz, SAME kabında sürükleyip bırakmaya izin veriyor. Davranışlar, MVVM – user819313

+0

ile sürükle ve bırak işlemlerini gerçekleştirmenin en iyi yoludur. Bunu doğru b/c olarak işaretleyerek MVVM'ye yardımcı olur. DimsContainer'a başvuran satırdan kurtulmak için gönderici parametresini kullandım ve ItemsControl olarak yayınladım. – KrisTrip

İlgili konular