I have an WPF app (.NET Framework 4.8) that has only two windows and no code except for code behind.
Here's the main window XAML:
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight">
<Button Click="ButtonBase_OnClick" Content="Click here" FontSize="100"/>
</Window>
Here's the code behind:
using System.Windows;
namespace WpfApp3
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
new Window1 {Left = this.Left + this.Width, Top = this.Top}.Show();
}
}
}
The second form, Window1
, has just one text block with no real code behind:
<Window x:Class="WpfApp3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight">
<TextBlock Text="New form" FontSize="50"/>
</Window>
namespace WpfApp3
{
public partial class Window1
{
public Window1()
{
InitializeComponent();
}
}
}
I expect that clicking the button should position Window1
to the right of MainWindow
, with no overlap or gap between those two (because Left = this.Left + this.Width
).
Why do I always get a visible gap between them?
The gap is a result of using MainWindow
's width. Left = this.Left
makes Window1
's and MainWindow
's left borders' positions match perfectly.
If I try putting Window1
immediately under MainWindow
by writing Top = this.Top + this.Height
, a similar (but slightly narrower) gap shows up between the MainWindow
's botton and Window1
's top.