Jonathan Wood
Jonathan Wood

Reputation: 67195

Window size does not match what I have in the designer

I'm not sure why it seems that every time I work with WPF, things are always much more difficult that WinForms or C/SDK.

In this case, the window looks like this in the designer.

enter image description here

But here's how it looks at run time.

enter image description here

And my XAML:

<Window x:Class="InsiderArticlesManager.AuthorWindow"
        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:InsiderArticlesManager"
        mc:Ignorable="d"
        Title="Set Author" Height="114" Width="341" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" SourceInitialized="Window_SourceInitialized" WindowStyle="SingleBorderWindow">
    <StackPanel>
        <Label Name="lblPrompt" Content="Select Author:" Margin="5,5,5,0" />
        <ComboBox Name="UserList" DisplayMemberPath="Email" Margin="5,0,5,5"></ComboBox>
        <WrapPanel HorizontalAlignment="Right">
            <Button Name="btnOk" Content="OK" Width="78" Margin="5,5,0,5" IsDefault="True" Click="Ok_Click" />
            <Button Name="btnCancel" Content="Cancel" Width="78" Margin="5,5,5,5" IsCancel="True" />
        </WrapPanel>
    </StackPanel>

</Window>

I thought the whole point of the designer was so that I could see how the window will look. Since it shows me something different, how do I know what size to set it?

Upvotes: 1

Views: 965

Answers (1)

Streamline
Streamline

Reputation: 982

You might want to set the Width and Height on the StackPanel and set the Window's SizeToContent to WidthAndHeight. That way the content is always visible, no matter the size of the window border (assuming you size your StackPanel correctly :)):

<Window x:Class="InsiderArticlesManager.AuthorWindow"
        ...
        SizeToContent="WidthAndHeight">
    <StackPanel Height="114" Width="341">
        ...
    </StackPanel>
</Window>

In your case the difference between design time and runtime occurs because the XAML designer window has a smaller window border size.

Upvotes: 3

Related Questions