Skip to content

Commit b1ff1b1

Browse files
committed
refactor: check for same context to get the templates in preview
1 parent cf1f508 commit b1ff1b1

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

src/GongSolutions.WPF.DragDrop/DragDropPreview.cs

+44-30
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,29 @@ public static bool HasDragDropPreview(IDragInfo dragInfo, UIElement visualTarget
183183
// Check for source template or template selector if there is no target one
184184
if (template is null && templateSelector is null)
185185
{
186-
template = isMultiSelection
187-
? DragDrop.TryGetDragAdornerMultiItemTemplate(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplate(visualSource, sender)
188-
: DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
189-
templateSelector = isMultiSelection
190-
? DragDrop.TryGetDragAdornerMultiItemTemplateSelector(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender)
191-
: DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);
192-
193-
var useDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
194-
if (useDefaultDragAdorner)
195-
{
196-
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
197-
}
186+
var sourceContext = DragDrop.GetDragDropContext(dragInfo.VisualSource);
187+
var targetContext = visualTarget != null ? DragDrop.GetDragDropContext(visualTarget) : null;
188+
var isSameContext = string.Equals(sourceContext, targetContext) || string.IsNullOrEmpty(targetContext);
198189

199-
if (template is not null)
190+
if (isSameContext)
200191
{
201-
templateSelector = null;
192+
template = isMultiSelection
193+
? DragDrop.TryGetDragAdornerMultiItemTemplate(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplate(visualSource, sender)
194+
: DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
195+
templateSelector = isMultiSelection
196+
? DragDrop.TryGetDragAdornerMultiItemTemplateSelector(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender)
197+
: DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);
198+
199+
var useDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
200+
if (useDefaultDragAdorner)
201+
{
202+
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
203+
}
204+
205+
if (template is not null)
206+
{
207+
templateSelector = null;
208+
}
202209
}
203210
}
204211

@@ -232,24 +239,31 @@ public void UpdatePreviewPresenter(IDragInfo dragInfo, UIElement visualTarget, U
232239
// Get source template or template selector if there is no target one
233240
if (template is null && templateSelector is null)
234241
{
235-
template = isMultiSelection
236-
? DragDrop.TryGetDragAdornerMultiItemTemplate(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplate(visualSource, sender)
237-
: DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
238-
templateSelector = isMultiSelection
239-
? DragDrop.TryGetDragAdornerMultiItemTemplateSelector(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender)
240-
: DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);
241-
itemsPanel = DragDrop.TryGetDragAdornerItemsPanel(visualTarget, sender);
242-
243-
this.UseDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
244-
if (this.UseDefaultDragAdorner)
245-
{
246-
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
247-
this.UseDefaultDragAdorner = template is not null;
248-
}
242+
var sourceContext = DragDrop.GetDragDropContext(dragInfo.VisualSource);
243+
var targetContext = visualTarget != null ? DragDrop.GetDragDropContext(visualTarget) : null;
244+
var isSameContext = string.Equals(sourceContext, targetContext) || string.IsNullOrEmpty(targetContext);
249245

250-
if (template is not null)
246+
if (isSameContext)
251247
{
252-
templateSelector = null;
248+
template = isMultiSelection
249+
? DragDrop.TryGetDragAdornerMultiItemTemplate(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplate(visualSource, sender)
250+
: DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
251+
templateSelector = isMultiSelection
252+
? DragDrop.TryGetDragAdornerMultiItemTemplateSelector(visualSource, sender) ?? DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender)
253+
: DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);
254+
itemsPanel = DragDrop.TryGetDragAdornerItemsPanel(visualTarget, sender);
255+
256+
this.UseDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
257+
if (this.UseDefaultDragAdorner)
258+
{
259+
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
260+
this.UseDefaultDragAdorner = template is not null;
261+
}
262+
263+
if (template is not null)
264+
{
265+
templateSelector = null;
266+
}
253267
}
254268
}
255269

0 commit comments

Comments
 (0)