Skip to content

Commit

Permalink
add UnfilteredInsertIndex to DropInfo
Browse files Browse the repository at this point in the history
Gets the current insert position within the source (unfiltered).
This should be only used in a Drop action.
This works only correct with different objects (string, int, etc won't
work correct).
  • Loading branch information
punker76 committed Jan 16, 2015
1 parent 6c8f5f9 commit 51933a6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
23 changes: 6 additions & 17 deletions GongSolutions.Wpf.DragDrop/DefaultDropHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using GongSolutions.Wpf.DragDrop.Utilities;
using System.Windows.Controls;
using System.ComponentModel;

namespace GongSolutions.Wpf.DragDrop
{
Expand All @@ -22,20 +20,20 @@ public virtual void DragOver(IDropInfo dropInfo)

public virtual void Drop(IDropInfo dropInfo)
{
var insertIndex = dropInfo.InsertIndex;
var destinationList = GetList(dropInfo.TargetCollection);
var insertIndex = dropInfo.InsertIndex != dropInfo.UnfilteredInsertIndex ? dropInfo.UnfilteredInsertIndex : dropInfo.InsertIndex;
var destinationList = dropInfo.TargetCollection.ToList();
var data = ExtractData(dropInfo.Data);

if (dropInfo.DragInfo.VisualSource == dropInfo.VisualTarget) {
var sourceList = GetList(dropInfo.DragInfo.SourceCollection);
var sourceList = dropInfo.DragInfo.SourceCollection.ToList();

foreach (var o in data) {
var index = sourceList.IndexOf(o);

if (index != -1) {
sourceList.RemoveAt(index);

if (sourceList == destinationList && index < insertIndex) {
if (Equals(sourceList, destinationList) && index < insertIndex) {
--insertIndex;
}
}
Expand All @@ -59,7 +57,7 @@ public static bool CanAcceptData(IDropInfo dropInfo)
}

if (dropInfo.DragInfo.SourceCollection == dropInfo.TargetCollection) {
return GetList(dropInfo.TargetCollection) != null;
return dropInfo.TargetCollection.ToList() != null;
} else if (dropInfo.DragInfo.SourceCollection is ItemCollection) {
return false;
} else if (dropInfo.TargetCollection == null) {
Expand All @@ -82,15 +80,6 @@ public static IEnumerable ExtractData(object data)
}
}

public static IList GetList(IEnumerable enumerable)
{
if (enumerable is ICollectionView) {
return ((ICollectionView)enumerable).SourceCollection as IList;
} else {
return enumerable as IList;
}
}

protected static bool IsChildOf(UIElement targetItem, UIElement sourceItem)
{
var parent = ItemsControl.ItemsControlFromItemContainer(targetItem);
Expand Down
35 changes: 33 additions & 2 deletions GongSolutions.Wpf.DragDrop/DropInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using GongSolutions.Wpf.DragDrop.Utilities;
Expand All @@ -18,6 +19,9 @@ namespace GongSolutions.Wpf.DragDrop
/// </remarks>
public class DropInfo : IDropInfo
{
private ItemsControl itemParent = null;
private UIElement item = null;

/// <summary>
/// Initializes a new instance of the DropInfo class.
/// </summary>
Expand Down Expand Up @@ -57,7 +61,7 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)

if (this.VisualTarget is ItemsControl) {
var itemsControl = (ItemsControl)this.VisualTarget;
var item = itemsControl.GetItemContainerAt(this.DropPosition);
item = itemsControl.GetItemContainerAt(this.DropPosition);
var directlyOverItem = item != null;

this.TargetGroup = itemsControl.FindGroup(this.DropPosition);
Expand All @@ -70,7 +74,7 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
}

if (item != null) {
var itemParent = ItemsControl.ItemsControlFromItemContainer(item);
itemParent = ItemsControl.ItemsControlFromItemContainer(item);

this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;
Expand Down Expand Up @@ -170,6 +174,33 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
/// </summary>
public int InsertIndex { get; private set; }

/// <summary>
/// Gets the current insert position within the source (unfiltered) <see cref="TargetCollection"/>.
/// </summary>
/// <remarks>
/// This should be only used in a Drop action.
/// This works only correct with different objects (string, int, etc won't work correct).
/// </remarks>
public int UnfilteredInsertIndex
{
get
{
var insertIndex = this.InsertIndex;
if (itemParent != null) {
var itemSourceAsList = itemParent.ItemsSource.ToList();
if (itemSourceAsList != null && itemParent.Items != null && itemParent.Items.Count != itemSourceAsList.Count) {
if (insertIndex >= 0 && insertIndex < itemParent.Items.Count) {
var indexOf = itemSourceAsList.IndexOf(itemParent.Items[insertIndex]);
if (indexOf >= 0) {
return indexOf;
}
}
}
}
return insertIndex;
}
}

/// <summary>
/// Gets the collection that the target ItemsControl is bound to.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions GongSolutions.Wpf.DragDrop/IDropInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public interface IDropInfo
/// </summary>
int InsertIndex { get; }

/// <summary>
/// Gets the current insert position within the source (unfiltered) <see cref="TargetCollection"/>.
/// </summary>
/// <remarks>
/// This should be only used in a Drop action.
/// This works only correct with different objects (string, int, etc won't work correct).
/// </remarks>
int UnfilteredInsertIndex { get; }

/// <summary>
/// Gets the collection that the target ItemsControl is bound to.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions GongSolutions.Wpf.DragDrop/Utilities/TypeUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Collections;

Expand Down Expand Up @@ -74,5 +75,20 @@ public static Type GetCommonBaseClass(Type[] types)

return ret;
}

/// <summary>
/// Gets the enumerable as list.
/// If enumerable is an ICollectionView then it returns the SourceCollection as list.
/// </summary>
/// <param name="enumerable">The enumerable.</param>
/// <returns>Returns a list.</returns>
public static IList ToList(this IEnumerable enumerable)
{
if (enumerable is ICollectionView) {
return ((ICollectionView)enumerable).SourceCollection as IList;
} else {
return enumerable as IList;
}
}
}
}

0 comments on commit 51933a6

Please sign in to comment.