question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong DragAdorner offset in TreeView

See original GitHub issue

What steps will reproduce this issue?

  • Create a TreeView with parent & child items
  • Collapse childs
  • Drag with dd:DragDrop.UseDefaultDragAdorner="True"

2020-03-13_14-32-07 (1)

Expected outcome

No offset of the dragged items

Environment

  • GongSolutions.WPF.DragDrop 2.2.0
  • Windows OS 10
  • .NET Framework 4.7.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Metritutuscommented, Aug 11, 2021

It’s not a cache mechanism. What’s happening is VisualTreeHelper.GetDescendantBounds() does not take into account the bounds (or lack thereof) of collapsed Visual elements.

I happened upon this whilst trying to figure out why the Highlight adorner would highlight the wrong amount of area when I’d collapsed a DataGridRow containing row details.

I’ve raised a case on the official WPF repo here.

In the mean time I have applied a workaround I have developed (detailed in the above case) to a separate branch, forked from develop, here.

I’ve also made alterations to DragDropExtensions.cs to try and correctly render the dragged item, which also fixes this issue (#346).

I have not created a pull request for all this yet as the reflection-based workaround is fairly horrifying, although it does appear to work as desired. With that being said however, I’m not sure if there is a better way of working around this that wouldn’t risk the behaviour starting to increasingly drift from the original VisualTreeHelper.GetDescendantBounds() method.

Let me know if you do actually want me to create a pull-request with these changes, @punker76.

0reactions
Metritutuscommented, Aug 12, 2021

@punker76 The change in my original code was general purpose, it would work for the evaluation of any Visual with collapsed descendants (I’ve tested it working with DataGrid rows with collapsed row details). The only other thing I needed to change for this issue was how the drag-drop preview image was being rendered (which fixes the preview looking squashed when dragging around a Visual containing collapsed components such as a TreeViewItem after using the new GetVisibleDescendantBounds() method).

I have also had a look at your change. Because you’re using VisualTreeHelper.GetDescendantBounds(), both the Width and the Location properties you’re using would be affected by collapsed elements and thus potentially misaligned.

Looking at where you’re using DesiredSize though, you could probably just replace what you have there with new Rect(0, 0, uiElement.DesiredSize.Width, uiElement.DesiredSize.Height).

However, although DesiredSize does seem to work in this case, it looks like it’s calculated in a very different manner to how GetDescendantBounds() operates, not to mention that it depends on a Visual being a UIElement in order to correctly account for collapsed children.

On top of this, my understanding is that DesiredSize refers to how big a UIElement wishes to be, not how big it actually is after the whole layout process has completed. ActualWidth and ActualHeight would probably be closer to what is needed, but they’re on FrameworkElement, which would narrow the useful usability of the function further still.

As far as I can see, the reflection solution seems to remain the best approach to this in terms of reliability and minimising functional changes. With any luck, Microsoft will come back with a positive response that either fixes this or adds a new method to VisualTreeHelper. If that is the case then I’ve mentioned in the case that I’m happy to try and put together a pull request for WPF for whatever change is needed to ultimately address all of this.

In the mean time, having slept on this for a bit, I’ll create a pull request with my changes for this repo. If WPF gets updated to resolve this then the reflection workaround can then be removed later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rearrange in treeview by drag and drop with placeholder
I want to rearrange the items in treeview(wpf, c#) by drag and drop, ... { IsHitTestVisible = false; } public DragAdorner(UIElement owner, ...
Read more >
Changing position of expanded items in TreeView
You cannot take out the TreeViewItem from its TreeView . Best you can do is modify its template to expand it horizontally.
Read more >
[Solved]-Drag and Drop with Drag Adorner-wpf
It seems that the Mouse pointer is over the Adorner and it takes it as Target. Setting the offset to a Point so...
Read more >
TreeView.ItemDrag Event (System.Windows.Forms)
The following code example demonstrates how to enable drag-and-drop operations within a TreeView control. In this example, any node can be dragged to ......
Read more >
Release History - punker76/gong-wpf-dragdrop GitHub Wiki
Displays the drag adorner all the time even when the DragDropEffect is None. Offset the adorner so that the cursor is sat at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found