Skip to content

Commit

Permalink
Merge pull request #393 from /issues/327
Browse files Browse the repository at this point in the history
Fix using correct template or template selector for source and target
  • Loading branch information
punker76 authored Sep 7, 2021
2 parents d6994c8 + 312c08f commit 135f161
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 31 deletions.
16 changes: 10 additions & 6 deletions src/GongSolutions.WPF.DragDrop/DragDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ private static DragDropPreview GetDragDropPreview(IDragInfo dragInfo, UIElement
return null;
}

var rootElement = TryGetRootElementFinder(sender).FindRoot(visualTarget ?? visualSource);

var preview = new DragDropPreview(rootElement, dragInfo, visualTarget ?? visualSource, sender);
if (preview.Child != null)
var hasDragDropPreview = DragDropPreview.HasDragDropPreview(dragInfo, visualTarget ?? visualSource, sender);
if (hasDragDropPreview)
{
preview.IsOpen = true;
return preview;
var rootElement = TryGetRootElementFinder(sender).FindRoot(visualTarget ?? visualSource);

var preview = new DragDropPreview(rootElement, dragInfo, visualTarget ?? visualSource, sender);
if (preview.Child != null)
{
preview.IsOpen = true;
return preview;
}
}

return null;
Expand Down
93 changes: 68 additions & 25 deletions src/GongSolutions.WPF.DragDrop/DragDropPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,62 +137,105 @@ protected override void OnOpened(EventArgs e)
}
}

public void UpdatePreviewPresenter(IDragInfo dragInfo, UIElement visualTarget, UIElement sender)
public static bool HasDragDropPreview(IDragInfo dragInfo, UIElement visualTarget, UIElement sender)
{
if (this.UseDefaultDragAdorner)
var visualSource = dragInfo?.VisualSource;
if (visualSource is null)
{
return;
return false;
}

var visualSource = dragInfo.VisualSource;

DataTemplate template = DragDrop.TryGetDropAdornerTemplate(visualTarget, sender) ?? DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
DataTemplateSelector templateSelector = DragDrop.TryGetDropAdornerTemplateSelector(visualTarget, sender) ?? DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);
// Check for target template or template selector
DataTemplate template = DragDrop.TryGetDropAdornerTemplate(visualTarget, sender);
DataTemplateSelector templateSelector = DragDrop.TryGetDropAdornerTemplateSelector(visualTarget, sender);

if (template is not null)
{
templateSelector = null;
}

this.SetCurrentValue(ItemTemplateProperty, template);
this.SetCurrentValue(ItemTemplateSelectorProperty, templateSelector);
}
// Check for source template or template selector if there is no target one
if (template is null && templateSelector is null)
{
template = DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
templateSelector = DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);

public UIElement CreatePreviewPresenter(IDragInfo dragInfo, UIElement visualTarget, UIElement sender)
{
var visualSource = dragInfo.VisualSource;
var useDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
if (useDefaultDragAdorner)
{
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
}

DataTemplate template = DragDrop.TryGetDropAdornerTemplate(visualTarget, sender) ?? DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
DataTemplateSelector templateSelector = DragDrop.TryGetDropAdornerTemplateSelector(visualTarget, sender) ?? DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);
if (template is not null)
{
templateSelector = null;
}
}

var useDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
var useVisualSourceItemSizeForDragAdorner = dragInfo.VisualSourceItem != null && DragDrop.GetUseVisualSourceItemSizeForDragAdorner(visualSource);
return template is not null || templateSelector is not null;
}

if (useDefaultDragAdorner)
public void UpdatePreviewPresenter(IDragInfo dragInfo, UIElement visualTarget, UIElement sender)
{
var visualSource = dragInfo?.VisualSource;
if (visualSource is null)
{
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
useDefaultDragAdorner = template is not null;
return;
}

// Get target template or template selector
DataTemplate template = DragDrop.TryGetDropAdornerTemplate(visualTarget, sender);
DataTemplateSelector templateSelector = DragDrop.TryGetDropAdornerTemplateSelector(visualTarget, sender);

if (template is not null)
{
templateSelector = null;
}

this.SetCurrentValue(ItemTemplateProperty, template);
// Get source template or template selector if there is no target one
if (template is null && templateSelector is null)
{
template = DragDrop.TryGetDragAdornerTemplate(visualSource, sender);
templateSelector = DragDrop.TryGetDragAdornerTemplateSelector(visualSource, sender);

this.UseDefaultDragAdorner = template is null && templateSelector is null && DragDrop.GetUseDefaultDragAdorner(visualSource);
if (this.UseDefaultDragAdorner)
{
template = dragInfo.VisualSourceItem.GetCaptureScreenDataTemplate(dragInfo.VisualSourceFlowDirection);
this.UseDefaultDragAdorner = template is not null;
}

if (template is not null)
{
templateSelector = null;
}
}

this.SetCurrentValue(ItemTemplateSelectorProperty, templateSelector);
this.SetCurrentValue(ItemTemplateProperty, template);
}

public UIElement CreatePreviewPresenter(IDragInfo dragInfo, UIElement visualTarget, UIElement sender)
{
var visualSource = dragInfo?.VisualSource;
if (visualSource is null)
{
return null;
}

var useVisualSourceItemSizeForDragAdorner = dragInfo.VisualSourceItem != null && DragDrop.GetUseVisualSourceItemSizeForDragAdorner(visualSource);

this.UseDefaultDragAdorner = useDefaultDragAdorner;
this.UpdatePreviewPresenter(dragInfo, visualTarget, sender);

UIElement adornment = null;

if (template != null || templateSelector != null)
if (this.ItemTemplate != null || this.ItemTemplateSelector != null)
{
if (dragInfo.Data is IEnumerable items && !(items is string))
{
var itemsCount = items.Cast<object>().Count();
var maxItemsCount = DragDrop.TryGetDragPreviewMaxItemsCount(dragInfo, sender);
if (!useDefaultDragAdorner && itemsCount <= maxItemsCount)
if (!this.UseDefaultDragAdorner && itemsCount <= maxItemsCount)
{
// sort items if necessary before creating the preview
var sorter = DragDrop.TryGetDragPreviewItemsSorter(dragInfo, sender);
Expand Down Expand Up @@ -242,7 +285,7 @@ public UIElement CreatePreviewPresenter(IDragInfo dragInfo, UIElement visualTarg
}
}

if (adornment != null && useDefaultDragAdorner)
if (adornment != null && this.UseDefaultDragAdorner)
{
adornment.Opacity = DragDrop.GetDefaultDragAdornerOpacity(visualSource);
}
Expand Down

0 comments on commit 135f161

Please sign in to comment.