Skip to content

Commit c89bd0b

Browse files
authored
Merge pull request #17 from SafeTwice/bugfix/fix-drag-behavior
Fix FluidWrapPanel drag'n'drop not working when the dragged element is a descendant of the panel child item
2 parents 0e3717e + 03a0229 commit c89bd0b

File tree

1 file changed

+29
-57
lines changed

1 file changed

+29
-57
lines changed

WPFSpark/FluidWrapPanel/FluidMouseDragBehavior.cs

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
//
2828

2929
using System.Windows;
30-
using System.Windows.Controls;
3130
using System.Windows.Input;
3231
using System.Windows.Media;
3332
using Microsoft.Xaml.Behaviors;
@@ -42,7 +41,7 @@ public class FluidMouseDragBehavior : Behavior<UIElement>
4241
#region Fields
4342

4443
FluidWrapPanel _parentFwPanel;
45-
ListBoxItem _parentLbItem;
44+
UIElement _fwPanelChild;
4645

4746
#endregion
4847

@@ -92,17 +91,11 @@ private void OnAssociatedObjectLoaded(object sender, RoutedEventArgs e)
9291
GetParentPanel();
9392

9493
// Subscribe to the Mouse down/move/up events
95-
if (_parentLbItem != null)
94+
if (_fwPanelChild != null)
9695
{
97-
_parentLbItem.PreviewMouseDown += OnPreviewMouseDown;
98-
_parentLbItem.PreviewMouseMove += OnPreviewMouseMove;
99-
_parentLbItem.PreviewMouseUp += OnPreviewMouseUp;
100-
}
101-
else
102-
{
103-
AssociatedObject.PreviewMouseDown += OnPreviewMouseDown;
104-
AssociatedObject.PreviewMouseMove += OnPreviewMouseMove;
105-
AssociatedObject.PreviewMouseUp += OnPreviewMouseUp;
96+
_fwPanelChild.PreviewMouseDown += OnPreviewMouseDown;
97+
_fwPanelChild.PreviewMouseMove += OnPreviewMouseMove;
98+
_fwPanelChild.PreviewMouseUp += OnPreviewMouseUp;
10699
}
107100
}
108101

@@ -113,27 +106,27 @@ private void OnAssociatedObjectLoaded(object sender, RoutedEventArgs e)
113106
/// </summary>
114107
private void GetParentPanel()
115108
{
116-
if ((AssociatedObject as FrameworkElement) == null)
117-
return;
118-
119-
var ancestor = (FrameworkElement) AssociatedObject;
109+
_fwPanelChild = AssociatedObject;
120110

121-
while (ancestor != null)
111+
while (_fwPanelChild != null)
122112
{
123-
if (ancestor is ListBoxItem)
113+
// Get the visual parent of the current item
114+
var parent = VisualTreeHelper.GetParent(_fwPanelChild) as UIElement;
115+
116+
if (parent == null)
124117
{
125-
_parentLbItem = ancestor as ListBoxItem;
118+
_fwPanelChild = null;
126119
}
127-
128-
if (ancestor is FluidWrapPanel)
120+
else if (parent is FluidWrapPanel)
129121
{
130-
_parentFwPanel = ancestor as FluidWrapPanel;
131-
// No need to go further up
122+
_parentFwPanel = (FluidWrapPanel) parent;
123+
// Search finished
132124
return;
133125
}
134-
135-
// Find the visual ancestor of the current item
136-
ancestor = VisualTreeHelper.GetParent(ancestor) as FrameworkElement;
126+
else
127+
{
128+
_fwPanelChild = parent;
129+
}
137130
}
138131
}
139132

@@ -143,17 +136,11 @@ protected override void OnDetaching()
143136
return;
144137

145138
((FrameworkElement) AssociatedObject).Loaded -= OnAssociatedObjectLoaded;
146-
if (_parentLbItem != null)
139+
if (_fwPanelChild != null)
147140
{
148-
_parentLbItem.PreviewMouseDown -= OnPreviewMouseDown;
149-
_parentLbItem.PreviewMouseMove -= OnPreviewMouseMove;
150-
_parentLbItem.PreviewMouseUp -= OnPreviewMouseUp;
151-
}
152-
else
153-
{
154-
AssociatedObject.PreviewMouseDown -= OnPreviewMouseDown;
155-
AssociatedObject.PreviewMouseMove -= OnPreviewMouseMove;
156-
AssociatedObject.PreviewMouseUp -= OnPreviewMouseUp;
141+
_fwPanelChild.PreviewMouseDown -= OnPreviewMouseDown;
142+
_fwPanelChild.PreviewMouseMove -= OnPreviewMouseMove;
143+
_fwPanelChild.PreviewMouseUp -= OnPreviewMouseUp;
157144
}
158145
}
159146

@@ -166,13 +153,8 @@ async void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
166153
if (e.ChangedButton != DragButton)
167154
return;
168155

169-
var position = _parentLbItem != null ? e.GetPosition(_parentLbItem) : e.GetPosition(AssociatedObject);
170-
171-
var fElem = AssociatedObject as FrameworkElement;
172-
if ((fElem != null) && (_parentFwPanel != null))
173-
{
174-
await _parentFwPanel.BeginFluidDragAsync(_parentLbItem ?? AssociatedObject, position);
175-
}
156+
var position = e.GetPosition(_fwPanelChild);
157+
await _parentFwPanel.BeginFluidDragAsync(_fwPanelChild, position);
176158
}
177159

178160
private async void OnPreviewMouseMove(object sender, MouseEventArgs e)
@@ -216,29 +198,19 @@ private async void OnPreviewMouseMove(object sender, MouseEventArgs e)
216198
if (!isDragging)
217199
return;
218200

219-
var position = _parentLbItem != null ? e.GetPosition(_parentLbItem) : e.GetPosition(AssociatedObject);
220-
221-
var fElem = AssociatedObject as FrameworkElement;
222-
if ((fElem == null) || (_parentFwPanel == null))
223-
return;
224-
201+
var position = e.GetPosition(_fwPanelChild);
225202
var positionInParent = e.GetPosition(_parentFwPanel);
226-
await _parentFwPanel.FluidDragAsync(_parentLbItem ?? AssociatedObject, position, positionInParent);
203+
await _parentFwPanel.FluidDragAsync(_fwPanelChild, position, positionInParent);
227204
}
228205

229206
private async void OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
230207
{
231208
if (e.ChangedButton != DragButton)
232209
return;
233210

234-
var position = _parentLbItem != null ? e.GetPosition(_parentLbItem) : e.GetPosition(AssociatedObject);
235-
236-
var fElem = AssociatedObject as FrameworkElement;
237-
if ((fElem == null) || (_parentFwPanel == null))
238-
return;
239-
211+
var position = e.GetPosition(_fwPanelChild);
240212
var positionInParent = e.GetPosition(_parentFwPanel);
241-
await _parentFwPanel.EndFluidDragAsync(_parentLbItem ?? AssociatedObject, position, positionInParent);
213+
await _parentFwPanel.EndFluidDragAsync(_fwPanelChild, position, positionInParent);
242214
}
243215

244216
#endregion

0 commit comments

Comments
 (0)