WPF MainWindow always slightly smaller than defined

Started by
10 comments, last by Dave Haylett 6 years, 2 months ago

I have a simple MainWindow.xaml definition (most of it is default):

<Window x:Name="MyProj" x:Class="MyProj.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyProj"
        mc:Ignorable="d"
        Title="MyProj" Width="1200" Height="900" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" />

Then I create the window in a code-behind with:

            w = new MainWindow();
            w.Show();
 

But when the Window appears upon app start it is slightly smaller than 1200x900. In fact to get a 1200x900 window I need to specify 1215x939 in the MainWindow.xaml.

At first I though I had to take the title bar and possible vertical scrollbar into consideration (!) but I don't have a scrollbar.

What's going on??

Advertisement

In WPF window Width and Height are suggested values. You're basically telling the layout engine what you'd like, but the final decision is up to it. 

See the remark on the Window.Height property page on MSDN

Edit: btw, I would be hesitant about starting a new project with WPF... its future is pretty uncertain.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
5 hours ago, ChaosEngine said:

Edit: btw, I would be hesitant about starting a new project with WPF... its future is pretty uncertain.

Theoretically you are right but any WPF developer would argue against it for some reasons ;)

 

You can use the properties MinWidth/MinHeight if you need the window to be at least that size.

If you instead need the window to be smaller, but start with the size, you  can specify it in the code-behind.

 

Thanks for the replies, no good though I'm afraid. I do have control over the width and height, it's just that the window ends up 15 pixels narrower and 39 pixels shorter than I want. If I set the size to 800x600, it ends up being 785x561!

All I'm adding to the Window once it's been created is an Image (which is 1200x900) to fit the window, onto which I'm blitting loads of graphics. It all works fine, except I'm being short-changed on the window size!

Window Width/Height includes all the Frame/Chrome/NonClient Area dimensions. Either find a way to exclude that (that's what ClientSize was in the good old WinForms era) or make your Window bigger by that amount.

1 hour ago, rlyeh said:

Window Width/Height includes all the Frame/Chrome/NonClient Area dimensions. Either find a way to exclude that (that's what ClientSize was in the good old WinForms era) or make your Window bigger by that amount.

Yeah, and that is also style dependent based on the OS, etc. 

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Hmm. That's weird. One suggestion is to set all MinWidth and MaxWidth and Width to the value you want (i.e. 1200) and the same for height. Is your desire is to get 1200x900 client area or total window area (i.e. including any decorations)?

I did try that in my frustration, but nope, it was always shorter and narrower. I'd like my client window to be 1200x900, but even taking the window title bar into account, it still doesn't explain why the window loses 15 pixels' width, given the window borders are only 2 or 3 pixels either side. Annoying and weird.

Setting the Width/Height of the Window will not guarantee you a certain required client area. To guarantee that, you have to do it by relying on an element within the window to act as your client area, and have the window size to that element. In the example below I used a top-level Grid element. You can see how the actual widths and heights of both the window and client area element are. Note the SizeToContent attribute on the Window.


<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		SizeToContent="WidthAndHeight"
		x:Name="window">
	<Grid x:Name="clientArea" Width="1200" Height="900">
		<StackPanel>
			<StackPanel Orientation="Horizontal">
				<TextBlock Text="ActualWidth: " />
				<TextBlock Text="{Binding ActualWidth, ElementName=window}" />
			</StackPanel>
			<StackPanel Orientation="Horizontal">
				<TextBlock Text="ActualHeight: " />
				<TextBlock Text="{Binding ActualHeight, ElementName=window}" />
			</StackPanel>
			<StackPanel Orientation="Horizontal">
				<TextBlock Text="ClientWidth: " />
				<TextBlock Text="{Binding ActualWidth, ElementName=clientArea}" />
			</StackPanel>
			<StackPanel Orientation="Horizontal">
				<TextBlock Text="ClientHeight: " />
				<TextBlock Text="{Binding ActualHeight, ElementName=clientArea}" />
			</StackPanel>
		</StackPanel>
	</Grid>
</Window>

This topic is closed to new replies.

Advertisement