tags:

views:

4476

answers:

2

hi there,

the WPF Popup control is nice, but somewhat limited in my opinion. is there a way to "drag" a popup around when it is opened (like with the DragMove() method of windows)?

can this be done without big problems or do i have to write a substitute for the popup class myself? thanks

+8  A: 

There is no DragMove for PopUp. Just a small work around, there is lot of improvements you can add to this.

<Popup x:Name="pop" IsOpen="True" Height="200" Placement="AbsolutePoint"  Width="200">
   <Rectangle Stretch="Fill" Fill="Red"/>            
</Popup>

In the code behind , add this mousemove event

   pop.MouseMove += new MouseEventHandler(pop_MouseMove);

   void pop_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            pop.PlacementRectangle = new Rect(new Point(e.GetPosition(this).X,
                e.GetPosition(this).Y),new Point(200,200));

        }
    }
Jobi Joy
**Ouch**! No, use a [`Thumb`](http://msdn.microsoft.com/en-us/library/ms749252%28VS.100%29.aspx) and its `DragDelta` event instead (opt. `DragStarted`/`DragCompleted` if you want to manage states). It's much more efficient, won't fragment the memory and will not risk to lose the mouse as the example above. That's how dragging is done properly :) See [here](http://www.codeproject.com/KB/WPF/TubePlanner.aspx) for an example.
RedGlyph
I agree , that solution I have given is just a hack, As you said DragDelta is more performant than MoveMove.
Jobi Joy
A: 

but this is not a good solution, because when a mouse is moved very fast, mouse pointer will move out of Popup, so mouse move will not be fired in popup, in this case the solution fails.

Jegan
This isn't really an answer and should probably be a comment on the solution you want to address. Also, my suggestion for this problem would be user education. Many commercial applications and some windows functions don't work if you drag too fast.
marr75