Skip to content

Commit f877268

Browse files
committed
enable drop targets if ItemsControl are in ItemsControl and the ItemsControl is empty
1 parent c680fcf commit f877268

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

Examples/DefaultsExample/MainWindow.xaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,5 +1307,40 @@
13071307
</ListBox>
13081308
</Grid>
13091309
</TabItem>
1310+
1311+
<TabItem Header="List in List Example">
1312+
<Grid>
1313+
<Grid.ColumnDefinitions>
1314+
<ColumnDefinition />
1315+
<ColumnDefinition />
1316+
</Grid.ColumnDefinitions>
1317+
1318+
<ItemsControl Grid.Column="0"
1319+
ItemsSource="{Binding Collection1}"
1320+
dd:DragDrop.IsDragSource="True"
1321+
dd:DragDrop.UseDefaultDragAdorner="True">
1322+
</ItemsControl>
1323+
1324+
<ListBox Grid.Column="1" x:Name="outer">
1325+
<Border BorderThickness="1" BorderBrush="Black">
1326+
<ListBox Height="200" Width="200" x:Name="inner1" Background="Aquamarine"
1327+
ItemsSource="{Binding Collection2}"
1328+
dd:DragDrop.IsDragSource="True"
1329+
dd:DragDrop.IsDropTarget="True"
1330+
dd:DragDrop.UseDefaultDragAdorner="True">
1331+
</ListBox>
1332+
</Border>
1333+
<Border BorderThickness="1" BorderBrush="Black">
1334+
<ListBox Height="200" Width="200" x:Name="inner2"
1335+
dd:DragDrop.IsDragSource="True"
1336+
dd:DragDrop.IsDropTarget="True"
1337+
dd:DragDrop.UseDefaultDragAdorner="True">
1338+
<ListBoxItem Content="Test1"></ListBoxItem>
1339+
</ListBox>
1340+
</Border>
1341+
</ListBox>
1342+
1343+
</Grid>
1344+
</TabItem>
13101345
</TabControl>
13111346
</Window>

GongSolutions.Wpf.DragDrop/DropInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
6767

6868
if (this.VisualTarget is ItemsControl) {
6969
var itemsControl = (ItemsControl)this.VisualTarget;
70+
//System.Diagnostics.Debug.WriteLine(">>> Name = {0}", itemsControl.Name);
71+
// get item under the mouse
7072
item = itemsControl.GetItemContainerAt(this.DropPosition);
7173
var directlyOverItem = item != null;
7274

@@ -75,6 +77,7 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
7577
this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();
7678

7779
if (item == null) {
80+
// ok, no item found, so maybe we can found an item at top, left, right or bottom
7881
item = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
7982
directlyOverItem = false;
8083
}
@@ -89,7 +92,8 @@ public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
8992
}
9093
}
9194

92-
if (item != null) {
95+
if (item != null)
96+
{
9397
itemParent = ItemsControl.ItemsControlFromItemContainer(item);
9498

9599
this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);

GongSolutions.Wpf.DragDrop/Utilities/ItemsControlExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static UIElement GetItemContainer(this ItemsControl itemsControl, UIEleme
8282
if (itemType != null) {
8383
return isItemContainer
8484
? (UIElement)child.GetVisualAncestor(itemType, itemsControl)
85-
: (UIElement)child.GetVisualAncestor(itemType);
85+
: (UIElement)child.GetVisualAncestor(itemType, itemsControl.GetType());
8686
}
8787

8888
return null;
@@ -123,7 +123,7 @@ public static UIElement GetItemContainerAt(this ItemsControl itemsControl, Point
123123
result => {
124124
var itemContainer = isItemContainer
125125
? result.VisualHit.GetVisualAncestor(itemContainerType, itemsControl)
126-
: result.VisualHit.GetVisualAncestor(itemContainerType);
126+
: result.VisualHit.GetVisualAncestor(itemContainerType, itemsControl.GetType());
127127
if (itemContainer != null && !hits.Contains(itemContainer) && ((UIElement)itemContainer).IsVisible == true) {
128128
hits.Add(itemContainer);
129129
}

GongSolutions.Wpf.DragDrop/Utilities/VisualTreeExtensions.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,24 @@ public static T GetVisualAncestor<T>(this DependencyObject d) where T : class
6868
return null;
6969
}
7070

71-
public static DependencyObject GetVisualAncestor(this DependencyObject d, Type type)
71+
/// <summary>
72+
/// find the visual ancestor item by type
73+
/// </summary>
74+
public static DependencyObject GetVisualAncestor(this DependencyObject d, Type itemSearchType, Type itemContainerSearchType)
7275
{
73-
var item = VisualTreeHelper.GetParent(d.FindVisualTreeRoot());
76+
var currentVisual = VisualTreeHelper.GetParent(d.FindVisualTreeRoot());
7477

75-
while (item != null && type != null) {
76-
if (item.GetType() == type || item.GetType().IsSubclassOf(type)) {
77-
return item;
78+
while (currentVisual != null && itemSearchType != null) {
79+
var currentVisualType = currentVisual.GetType();
80+
if (currentVisualType == itemSearchType || currentVisualType.IsSubclassOf(itemSearchType)) {
81+
return currentVisual;
7882
}
79-
item = VisualTreeHelper.GetParent(item);
83+
if (itemContainerSearchType != null && itemContainerSearchType.IsAssignableFrom(currentVisualType))
84+
{
85+
// ok, we found an ItemsControl (maybe an empty)
86+
return null;
87+
}
88+
currentVisual = VisualTreeHelper.GetParent(currentVisual);
8089
}
8190

8291
return null;
@@ -85,16 +94,16 @@ public static DependencyObject GetVisualAncestor(this DependencyObject d, Type t
8594
/// <summary>
8695
/// find the visual ancestor by type and go through the visual tree until the given itemsControl will be found
8796
/// </summary>
88-
public static DependencyObject GetVisualAncestor(this DependencyObject d, Type type, ItemsControl itemsControl)
97+
public static DependencyObject GetVisualAncestor(this DependencyObject d, Type itemSearchType, ItemsControl itemsControl)
8998
{
9099
var item = VisualTreeHelper.GetParent(d.FindVisualTreeRoot());
91100
DependencyObject lastFoundItemByType = null;
92101

93-
while (item != null && type != null) {
102+
while (item != null && itemSearchType != null) {
94103
if (item == itemsControl) {
95104
return lastFoundItemByType;
96105
}
97-
if ((item.GetType() == type || item.GetType().IsSubclassOf(type))
106+
if ((item.GetType() == itemSearchType || item.GetType().IsSubclassOf(itemSearchType))
98107
&& (itemsControl == null || itemsControl.ItemContainerGenerator.IndexFromContainer(item) != -1)) {
99108
lastFoundItemByType = item;
100109
}

0 commit comments

Comments
 (0)