Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping insertion adorner fix #107

Merged
merged 2 commits into from
Sep 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions GongSolutions.Wpf.DragDrop/DragInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using GongSolutions.Wpf.DragDrop.Utilities;

Expand Down Expand Up @@ -40,6 +41,7 @@ public DragInfo(object sender, MouseButtonEventArgs e)
if (sender is ItemsControl) {
var itemsControl = (ItemsControl)sender;

this.SourceGroup = itemsControl.FindGroup(this.DragStartPosition);
this.VisualSourceFlowDirection = itemsControl.GetItemsPanelFlowDirection();

var sourceItem = e.OriginalSource as UIElement; // If we can't cast object as a UIElement it might be a FrameworkContentElement, if so try and use its parent.
Expand Down Expand Up @@ -159,6 +161,11 @@ public DragInfo(object sender, MouseButtonEventArgs e)
/// </remarks>
public IEnumerable SourceItems { get; private set; }

/// <summary>
/// Gets the group from a dragged item if the drag is currently from an ItemsControl with groups.
/// </summary>
public CollectionViewGroup SourceGroup { get; private set; }

/// <summary>
/// Gets the control that initiated the drag.
/// </summary>
Expand Down
17 changes: 1 addition & 16 deletions GongSolutions.Wpf.DragDrop/DropInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
var item = itemsControl.GetItemContainerAt(this.DropPosition);
var directlyOverItem = item != null;

this.TargetGroup = this.FindGroup(itemsControl, this.DropPosition);
this.TargetGroup = itemsControl.FindGroup(this.DropPosition);
this.VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();
this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

Expand Down Expand Up @@ -122,21 +122,6 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
}
}

private CollectionViewGroup FindGroup(ItemsControl itemsControl, Point position)
{
var element = itemsControl.InputHitTest(position) as DependencyObject;

if (element != null) {
var groupItem = element.GetVisualAncestor<GroupItem>();

if (groupItem != null) {
return groupItem.Content as CollectionViewGroup;
}
}

return null;
}

/// <summary>
/// Gets the drag data.
/// </summary>
Expand Down
15 changes: 14 additions & 1 deletion GongSolutions.Wpf.DragDrop/DropTargetInsertionAdorner.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections;
Expand Down Expand Up @@ -33,6 +35,17 @@ protected override void OnRender(DrawingContext drawingContext)
}

var index = Math.Min(this.DropInfo.InsertIndex, itemParent.Items.Count - 1);

var lastItemInGroup = false;
var targetGroup = this.DropInfo.TargetGroup;
if (targetGroup != null && targetGroup.IsBottomLevel && this.DropInfo.InsertPosition.HasFlag(RelativeInsertPosition.AfterTargetItem)) {
var indexOf = targetGroup.Items.IndexOf(this.DropInfo.TargetItem);
lastItemInGroup = indexOf == targetGroup.ItemCount - 1;
if (lastItemInGroup) {
index--;
}
}

var itemContainer = (UIElement)itemParent.ItemContainerGenerator.ContainerFromIndex(index);

if (itemContainer != null) {
Expand All @@ -41,7 +54,7 @@ protected override void OnRender(DrawingContext drawingContext)
double rotation = 0;

if (this.DropInfo.VisualTargetOrientation == Orientation.Vertical) {
if (this.DropInfo.InsertIndex == itemParent.Items.Count) {
if (this.DropInfo.InsertIndex == itemParent.Items.Count || lastItemInGroup) {
itemRect.Y += itemContainer.RenderSize.Height;
}

Expand Down
6 changes: 6 additions & 0 deletions GongSolutions.Wpf.DragDrop/IDragInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace GongSolutions.Wpf.DragDrop
Expand Down Expand Up @@ -72,6 +73,11 @@ public interface IDragInfo
/// </remarks>
IEnumerable SourceItems { get; }

/// <summary>
/// Gets the group from a dragged item if the drag is currently from an ItemsControl with groups.
/// </summary>
CollectionViewGroup SourceGroup { get; }

/// <summary>
/// Gets the control that initiated the drag.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions GongSolutions.Wpf.DragDrop/Utilities/ItemsControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Reflection;
using System.Collections;
Expand All @@ -17,6 +18,21 @@ namespace GongSolutions.Wpf.DragDrop.Utilities
{
public static class ItemsControlExtensions
{
public static CollectionViewGroup FindGroup(this ItemsControl itemsControl, Point position)
{
var element = itemsControl.InputHitTest(position) as DependencyObject;

if (element != null) {
var groupItem = element.GetVisualAncestor<GroupItem>();

if (groupItem != null) {
return groupItem.Content as CollectionViewGroup;
}
}

return null;
}

public static bool CanSelectMultipleItems(this ItemsControl itemsControl)
{
if (itemsControl is MultiSelector) {
Expand Down