tags:

views:

846

answers:

3

My main window has spawned a child window that's positioned on top to look like part of the main. I would like to move the child window in sync with the main but I'm not sure how.

My main window has my own title bar which event MouseLeftButtonDown calls this function:

public void DragWindow(object sender, MouseButtonEventArgs args)
{
     DragMove();
     UpdateChildWindowPosition();
}

This results in DragMove() executing on the main window moves the main window alone as I drag the title bar. UpdateChildWindowPosition() is not executed until I release the mouse at which it reads some element coordinates and sets the child window position - you see the child window snap to the location which is not desired.

How can I have the child window move in sync with the main window?

+1  A: 

You can use the window's Left and Top properties to place the secondary window. Here's a rough bit of code as an example:

In the MainWindow code:

        mDebugWindow.Left = this.Left + this.ActualWidth;
        mDebugWindow.Top = this.Top;

In this case mDebugWindow is my child window. This code tells it to sit on the right hand side of the main window, with the tops of the windows aligned.

Hope this helps.

George Sealy
Thanks, this is similar to the code in my UpdateChildWindowPosition(). It works however it doesn't work in realtime as I drag my main window around. It only gets executed when I release my drag causing the second window to be stationary as I drag and snap into place when I release.
Jippers
A: 

Can you expose a DragMove() method for the "child" form? Then just call it from the parent?

public void DragWindow(object sender, MouseButtonEventArgs args)
{
     DragMove();
     UpdatePosition();

     childForm.DragMove();
     childForm.UpdatePosition();
}

or if you are just using the mouse position to determine where to move the form, do the same calculations in your current DragMove() method and change the Form.Location property of your child form also?

scottm
Sorry I wasn't clearer. I've updated my question. DragMove() is a built in WPF function call to move the window on which I'm dragging. It stays executing until I release the drag, at which UpdatePosition is called and snaps the child window to a location.
Jippers
+1  A: 

AH HAH!

My Main window has an event LocationChanged which I've tied to my UpdateChildWindowPosition(). LocationChange fires when (duh) the location changes so it's continuously firing as I move the window when DragMove() is being executed in my DragWindow posted above.

Jippers