Skip to content

Commit

Permalink
new method TryGetNextAncestorDropTargetElement
Browse files Browse the repository at this point in the history
used in DropInfo
  • Loading branch information
punker76 committed Mar 17, 2015
1 parent 3cce250 commit a850796
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
17 changes: 8 additions & 9 deletions GongSolutions.Wpf.DragDrop/DropInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
this.KeyStates = e.KeyStates;

this.VisualTarget = sender as UIElement;
// if drop target isn't a ItemsControl
if (!(this.VisualTarget is ItemsControl)) {
// try to find next ItemsControl
var itemsControl = VisualTreeExtensions.GetVisualAncestor<ItemsControl>(this.VisualTarget);
if (itemsControl != null) {
// now check if this ItemsControl is a drop target
if (DragDrop.GetIsDropTarget(itemsControl)) {
this.VisualTarget = itemsControl;
}
// if there is no drop target, find another
if (!this.VisualTarget.IsDropTarget())
{
// try to find next element
var element = this.VisualTarget.TryGetNextAncestorDropTargetElement();
if (element != null)
{
this.VisualTarget = element;
}
}
// visual target can be null, so give us a point...
Expand Down
23 changes: 23 additions & 0 deletions GongSolutions.Wpf.DragDrop/Utilities/VisualTreeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ namespace GongSolutions.Wpf.DragDrop.Utilities
{
public static class VisualTreeExtensions
{
/// <summary>
/// Gets the next ancestor element which is a drop target.
/// </summary>
/// <param name="element">The start element.</param>
/// <returns>The first element which is a drop target.</returns>
public static UIElement TryGetNextAncestorDropTargetElement(this UIElement element)
{
if (element == null)
{
return null;
}
var ancestor = element.GetVisualAncestor<UIElement>();
while (ancestor != null)
{
if (ancestor.IsDropTarget())
{
return ancestor;
}
ancestor = ancestor.GetVisualAncestor<UIElement>();
}
return null;
}

internal static DependencyObject FindVisualTreeRoot(this DependencyObject d)
{
var current = d;
Expand Down

0 comments on commit a850796

Please sign in to comment.