locked
What's up with DragLeave and e.GetPosition RRS feed

  • Question

  • Hello,

    I actually have two questions related to drag/drop.  The first one is that I noticed DragLeave does not get fired for a parent element, if it is called for a child elements that fills it.  For example, lets say I have a simple dockpanel with a rectangle inside of it:

    <DockPanel AllowDrop="True" Background="AliceBlue" Grid.Column="1"  
                       Name="dockPanel1" Height="100" Width="200"  
                       DragLeave="dockPanel1_DragLeave"
                <Rectangle Name="rectangle1" Stroke="Black" Fill="Red" Width="100" /> 
            </DockPanel> 

    You'll notice that the heights of the rectangle and the dockpanel are the same.  The width's are different - the child rectangle is half the width of the parent.  I did this on purpose.  If you were to have this dockpanel be your "drop zone" in a drag and drop application, I would expect dockPanel1_DragLeave to be called whenever you actually leave the dockpanel.  This is not always the case.

    As I said earlier, the heights of the dockpanel and rectangle are both 100.  So... in this case, if you are dragging an item, and leave via the top or the bottom of the rectangle, the dockPanel1_DragLeave event only gets called once.  By writing to the console, I determined that it's original source is the rectangle (e.OriginalSource).

    Ok... so that really sucks.  I left my rectangle AND my dockpanel at the same time (they are the same height), but I only got an original source of Rectangle.  The dockpanel did not fire the same event.  So why is this a problem?  Well, dockPanel_DragLeave will fire as well if you leave the rectangle from the left or right.  In other words, while dragging, move the mouse off of the rectangle, and into the dockpanel (remember I set up the dockpanel to be twice as wide as the rectangle).

    The result?  By moving off of my rectangle from the top or bottom, I have moved out of both the rectangle and the dockpanel.  By moving out of my rectangle via the left or right I have ONLY moved out of the rectangle, and not the dockpanel.  So how can I determine based on a single DragLeave being called with an originalSource of rectangle if I am actually outside of the dockpanel or not?

    As far as I can tell, I can't.  Maybe someone has an easy solution, but I couldn't find one.  So I moved on to hittesting.  Simple enough right?  In the dragLeave event, use e.GetPosition and do a hittest to see if my element was in the region.  Well, maybe someone has an explanation but I get some seriously weird numbers when using e.GetPosition from within a DragLeave event.  Look at this code:

    private void dockPanel1_DragLeave (object sender, DragEventArgs e) 
          { 
             Point p = e.GetPosition (this.dockPanel1); 
             Console.WriteLine ("Point relative to dockpanel: " + p.X + "," + p.Y); 
          } 

    Lets say I actually left the dockpanel itself, from the left.  You'd think that I would get a point that looks something like x=-3 (my mouse is slightly to the left of the dockpanel now).  I don't.  I get a point who's x coordinate looks something like 152.  I just moved left of the dockpanel, and got a point relative to it, and my x coordinate is now 152.  What?  It actually looks like when performing a DragLeave, i get the position relative to the parent that my mouse is now within.  In my case, I moved out of the dockpanel.  I am now within a grid (that I didn't show in the code).  152 looks roughly like my mouse position according to the grid.

    Has anyone tested this out?  Is there something I'm missing?  Sorry I guess my two questions sort of got lost. Here they are again to sum up:

    1.)  Why doesn't DragLeave get called twice, once for my rectangle, once for my DockPanel if I leave via the top or bottom.
    2.)  Why is e.GetPosition screwed up in DragLeave (it appears fine, exactly as I would expect in DragOver).

    If anyone could help me out here I'd be grateful.  I realise I didn't provide too much code here, so if you would like to see a more complete application, please let me know.

    Thanks,

    huja
    Friday, November 7, 2008 8:58 PM

All replies

  • Good questions! I'm also looking into this at the moment (just as perplexed) - if I find anything I'll post it
    Wednesday, January 21, 2009 9:49 PM