Wrong DragAdorner offset in TreeView
See original GitHub issueWhat steps will reproduce this issue?
- Create a TreeView with parent & child items
- Collapse childs
- Drag with
dd:DragDrop.UseDefaultDragAdorner="True"
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:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s not a cache mechanism. What’s happening is
VisualTreeHelper.GetDescendantBounds()
does not take into account the bounds (or lack thereof) of collapsedVisual
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 aDataGridRow
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.
@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 withDataGrid
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 aVisual
containing collapsed components such as aTreeViewItem
after using the newGetVisibleDescendantBounds()
method).I have also had a look at your change. Because you’re using
VisualTreeHelper.GetDescendantBounds()
, both theWidth
and theLocation
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 withnew 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 howGetDescendantBounds()
operates, not to mention that it depends on aVisual
being aUIElement
in order to correctly account for collapsed children.On top of this, my understanding is that
DesiredSize
refers to how big aUIElement
wishes to be, not how big it actually is after the whole layout process has completed.ActualWidth
andActualHeight
would probably be closer to what is needed, but they’re onFrameworkElement
, 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.