Move two WPF windows at once?

In my main window, a child window appeared on top to look like part of the main one. I would like to move the child window in sync with the main one, but I'm not sure how to do this.

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

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

This results in DragMove () being executed in the main window only moving the main window when I drag the title bar. UpdateChildWindowPosition () is not executed until I release the mouse in which it reads some coordinates of the element and sets the position of the child window - you see the binding of the child window to a place that is undesirable.

How can I synchronize a child window with the main window?

+6
c # wpf
source share
4 answers

AH HAH!

There is a LocationChanged event in my main window, which I bound to my UpdateChildWindowPosition (). LocationChange fires when (duh) the location changes, so it fires continuously when I move the window when DragMove () is executed in my DragWindow above.

+5
source share

You can use the Left and Top windows to place the secondary window. Here is a sample code example:

In MainWindow code:

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

In this case, mDebugWindow is my child window. This code says that it sits to the right of the main window, and the tops of the windows are aligned.

Hope this helps.

+3
source share

Is it possible to set the DragMove () method for a "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 just use the mouse position to determine where to move the form, do the same calculations in the current DragMove () method and change the Form.Location property of your child form as well?

0
source share

Here is what I did:

First I set the parent instance as the owner of the Child window (make an instance by setting it in the MainWindow class public static MainWindow instance; then instance = this; ):

 public ChildWindow() { Owner = MainWindow.instance; InitializeComponent(); } 

Then I add an event handler in the parent class to fire when the parent moves:

 public MainWindow() { InitializeComponent(); LocationChanged += new EventHandler(Window_LocationChanged); } 

Now I look at all the windows omitted by MainWindow to reset their margin:

 private void Window_LocationChanged(object sender, EventArgs e) { foreach (Window win in this.OwnedWindows) { win.Top = this.Top + ((this.ActualHeight - win.ActualHeight) / 2); win.Left = this.Left + ((this.ActualWidth - win.ActualWidth) / 2); } } 
0
source share

All Articles