27
27
//
28
28
29
29
using System . Windows ;
30
- using System . Windows . Controls ;
31
30
using System . Windows . Input ;
32
31
using System . Windows . Media ;
33
32
using Microsoft . Xaml . Behaviors ;
@@ -42,7 +41,7 @@ public class FluidMouseDragBehavior : Behavior<UIElement>
42
41
#region Fields
43
42
44
43
FluidWrapPanel _parentFwPanel ;
45
- ListBoxItem _parentLbItem ;
44
+ UIElement _fwPanelChild ;
46
45
47
46
#endregion
48
47
@@ -92,17 +91,11 @@ private void OnAssociatedObjectLoaded(object sender, RoutedEventArgs e)
92
91
GetParentPanel ( ) ;
93
92
94
93
// Subscribe to the Mouse down/move/up events
95
- if ( _parentLbItem != null )
94
+ if ( _fwPanelChild != null )
96
95
{
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 ;
106
99
}
107
100
}
108
101
@@ -113,27 +106,27 @@ private void OnAssociatedObjectLoaded(object sender, RoutedEventArgs e)
113
106
/// </summary>
114
107
private void GetParentPanel ( )
115
108
{
116
- if ( ( AssociatedObject as FrameworkElement ) == null )
117
- return ;
118
-
119
- var ancestor = ( FrameworkElement ) AssociatedObject ;
109
+ _fwPanelChild = AssociatedObject ;
120
110
121
- while ( ancestor != null )
111
+ while ( _fwPanelChild != null )
122
112
{
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 )
124
117
{
125
- _parentLbItem = ancestor as ListBoxItem ;
118
+ _fwPanelChild = null ;
126
119
}
127
-
128
- if ( ancestor is FluidWrapPanel )
120
+ else if ( parent is FluidWrapPanel )
129
121
{
130
- _parentFwPanel = ancestor as FluidWrapPanel ;
131
- // No need to go further up
122
+ _parentFwPanel = ( FluidWrapPanel ) parent ;
123
+ // Search finished
132
124
return ;
133
125
}
134
-
135
- // Find the visual ancestor of the current item
136
- ancestor = VisualTreeHelper . GetParent ( ancestor ) as FrameworkElement ;
126
+ else
127
+ {
128
+ _fwPanelChild = parent ;
129
+ }
137
130
}
138
131
}
139
132
@@ -143,17 +136,11 @@ protected override void OnDetaching()
143
136
return ;
144
137
145
138
( ( FrameworkElement ) AssociatedObject ) . Loaded -= OnAssociatedObjectLoaded ;
146
- if ( _parentLbItem != null )
139
+ if ( _fwPanelChild != null )
147
140
{
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 ;
157
144
}
158
145
}
159
146
@@ -166,13 +153,8 @@ async void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
166
153
if ( e . ChangedButton != DragButton )
167
154
return ;
168
155
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 ) ;
176
158
}
177
159
178
160
private async void OnPreviewMouseMove ( object sender , MouseEventArgs e )
@@ -216,29 +198,19 @@ private async void OnPreviewMouseMove(object sender, MouseEventArgs e)
216
198
if ( ! isDragging )
217
199
return ;
218
200
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 ) ;
225
202
var positionInParent = e . GetPosition ( _parentFwPanel ) ;
226
- await _parentFwPanel . FluidDragAsync ( _parentLbItem ?? AssociatedObject , position , positionInParent ) ;
203
+ await _parentFwPanel . FluidDragAsync ( _fwPanelChild , position , positionInParent ) ;
227
204
}
228
205
229
206
private async void OnPreviewMouseUp ( object sender , MouseButtonEventArgs e )
230
207
{
231
208
if ( e . ChangedButton != DragButton )
232
209
return ;
233
210
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 ) ;
240
212
var positionInParent = e . GetPosition ( _parentFwPanel ) ;
241
- await _parentFwPanel . EndFluidDragAsync ( _parentLbItem ?? AssociatedObject , position , positionInParent ) ;
213
+ await _parentFwPanel . EndFluidDragAsync ( _fwPanelChild , position , positionInParent ) ;
242
214
}
243
215
244
216
#endregion
0 commit comments