4

I am working with a WPF application that uses alot of drag and drop. Everything is working fine, with exception of ListBoxItems. I have a ListBox with ListBoxItems that can be dragged to another target( a StackPanel). The problem is, when I drag the cursor outside the ListBox, I cant see the Adorner that I have setup with the ListBoxItem?

I know this is a common problem, but I am just not sure how to fix it. Is there something that I need to do to allow me to drag outside of the ListBox control?

Below I have attached what the UI looks like so far. As you can see, there is a ListBox on the bottom left. When I drag an item, the adorner appears, and follows the cursor around while the cursor is over the ListBox, but if I try to move the cursor away from the listbox, the Adorner seems to almost go under the other controls(zIndex?). enter image description here

Edit - Solution I have changed the code to handle the AdornerLayer relative to the window as oppose to relative to the AdornedElement

So I changed

    layer = AdornerLayer.GetAdornerLayer(_originalElement);

to

    layer = AdornerLayer.GetAdornerLayer(this);

This solved the problem of the ScrollViwer clipping the AdornerLayer

TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208

1 Answers1

4

The ListBox (or, to be specific, the ScrollViewer within the listbox) clips any adorners attached to it's children. This is done to ensure that adorners for items scrolled out of view are not shown. To get around this, you need to explicitly put thing in the Window's adorner and not that of the listbox or listboxitem

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • this makes sense, do you know where I can find an example of how to do this, or do you have any suggestions? Another option I thought of is to take away the scrollviewer clipping, by using a StackPanel for my items instead of a ListBox. – TheJediCowboy Feb 17 '11 at 15:22
  • 1
    I have been able to solve this problem. I have shown the code I replaced above. Thank you for pointing me in the right direction! – TheJediCowboy Feb 17 '11 at 16:09
  • Calling AdornerLayer.GetAdornerLayer(mainWindow) will get null returned. You can refer to [link](https://social.msdn.microsoft.com/Forums/vstudio/en-US/c5920146-9707-45fa-8db3-58f8f84f80ca/my-main-windows-adornerlayer-gets-created-when?forum=wpf) – YantingChen Jun 03 '19 at 09:17