Search code examples
wpfwindow

WPF window.Left +window.Width shows wrong


I want to show the second window near the first one, but WPF window.Left+window.Width don't helped me, look the image example.

Image example


Solution

  • First things first: I'd recommend to use Window.ActualWidth since this will give you the size that the rendered window actually takes up, while Window.Width only gives you the size that the window requested from the layout system.

    Anyway, this won't solve your problem. The problem here is that the width of a window contains both the width of the client area of the window as well as its non-client area, like the borders (and some more stuff I don't really know much about).

    So what you can try to do instead is to align the second window to the client area of the first window and take the window border widths into account, like this:

    window.Left = this.Left
        + (this.Content as FrameworkElement).ActualWidth
        + 2 * SystemParameters.BorderWidth;