How to give a child window bounds to not go outside the MainWindow in WPF

1.3k views Asked by At

I have a simple question really but I can't seem to find it. I am using WPF and I am creating a child window (Class Window) which displays a simple view. Now I can move the window around however what I would want is that it cannot move OUTSIDE the main window. (I think the Popup control has that behavior if I am not mistaking). Any easy way I can do that ?

Let me know if this isn't clear.

Thanks in advance.

Kind regards, Qwin

1

There are 1 answers

1
ReeganLourduraj On BEST ANSWER

Made you parent window as owner of child window and adjust the location

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


ChildWindow win = new ChildWindow();
   win.Owner = this;
   win.Show();

private void Window_LocationChanged(object sender, EventArgs e)
  {
   Console.WriteLine("LocationChanged - ({0},{1})", this.Top, this.Left);
   foreach (Window win in this.OwnedWindows)
   {
    win.Top = this.Top + 100;
    win.Left = this.Left + 100;
   }
  }