Form size at startup

Labelsoft
Labelsoft used Ask the Experts™
on
Dear Experts,

I am new to WPF and I am trying to make my first application. I am experimenting with xaml right now and I have run into the following problem:

When I start up the form it is not the size I would expect, but when I click the form it pops to the size as I would expect it. I do not understand this behaviour. Does anyone know what is causing this?

I have included the xaml that I am using.
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:AutoUpdateMonitorVML;assembly=AutoUpdateMonitorVML"
    Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:AutoUpdateMonitorVML;assembly=AutoUpdateMonitorVML" WindowStartupLocation="CenterScreen" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" d:DesignHeight="248" d:DesignWidth="525" SizeToContent="WidthAndHeight" Width="525" Height="248">
    <DockPanel Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="DockPanel1" VerticalAlignment="Stretch" Width="Auto" ForceCursor="False" LastChildFill="True">
        <Menu Height="20" Name="MainMenu" DockPanel.Dock="Top" VerticalAlignment="Top">
            <MenuItem Name="FileMenuItem" Header="Bestand">
                <MenuItem Name="RefreshServerMenuItem" Header="Refresh server" Click="MenuItem_Click" />
                <MenuItem Name="RefreshAllMenuItem" Header="Refresh all" Click="MenuItem_Click" />
                <MenuItem Name="UpdateServerMenuItem" Header="Update server" Click="MenuItem_Click" />
                <MenuItem Name="UpdateAllMenuItem" Header="Update all" Click="MenuItem_Click" />
                <Separator />
                <MenuItem Name="CloseMenuItem" Header="Afsluiten" Click="MenuItem_Click" />
            </MenuItem>
            <MenuItem Name="ToolsMenuItem" Header="Tools">
                <MenuItem Name="ManageServersMenuItem" Header="Refresh server" Click="MenuItem_Click" />
                <MenuItem Name="ManageDirectoryTypesMenuItem" Header="Refresh all" Click="MenuItem_Click" />
                <MenuItem Name="ManageSourceMenuItem" Header="Update server" Click="MenuItem_Click" />
            </MenuItem>
        </Menu>
        <DockPanel LastChildFill="True">
            <ToolBarPanel DockPanel.Dock="Top">
                <ToolBar />
            </ToolBarPanel>
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Name="Left" Width="2*" > </ColumnDefinition>
                    <ColumnDefinition Name="Middle" Width="3*"> </ColumnDefinition>
                    <ColumnDefinition Name="Right" Width="1*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <ListView Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="ServerListView" VerticalAlignment="Stretch" Width="Auto" ItemsSource="{Binding Servers}" Grid.Column="0">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Icoontje" DisplayMemberBinding="{Binding ID}"/>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                        <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Name}"/>
                    </GridView>
                </ListView.View>
                <!--<ListView.Resources>
                <DataTemplate DataType="{x:Type local:ServerVM}" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="icoontje" />
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text="status" />
                    </StackPanel>
                </DataTemplate>
            </ListView.Resources>-->
            </ListView>
            <TreeView Grid.Column="1" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            </TreeView>
            <TreeView Grid.Column="2" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            </TreeView>
            <GridSplitter Grid.Column="1" Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Name="GridSplitter1" VerticalAlignment="Stretch" Width="1" />
            <GridSplitter Grid.Column="2" Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Name="GridSplitter2" VerticalAlignment="Stretch" Width="1" />
            </Grid>
        </DockPanel>
    </DockPanel>
</Window>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Labelsoft;

I am just learning WPF myself and when I past your xaml into a project I was unable to compile the code because it made reference to what looks like an Expression Blend dll of which I do not have. After removing the reference and its dependency properties it look like what I think should be the start up size.

The code snippet below is what I changed your code to be as well as removing the using statement AutoUpdateMonitorVML in the MainWindow.xaml.cs.

I also added these two properties, MinHeight="248" MinWidth="525", which will not allow the form to go below that Width and Height. Now that I am thinking about it if you set the forms MinHeight and MinWidth without removing any code it may resolve your issue.

Fernando

Fernando
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         WindowStartupLocation="CenterScreen" VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" MinHeight="248" MinWidth="525" SizeToContent="WidthAndHeight" Width="525" Height="248">
    <DockPanel Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="DockPanel1" VerticalAlignment="Stretch" Width="Auto" ForceCursor="False" LastChildFill="True">
        <Menu Height="20" Name="MainMenu" DockPanel.Dock="Top" VerticalAlignment="Top">
            <MenuItem Name="FileMenuItem" Header="Bestand">
                <MenuItem Name="RefreshServerMenuItem" Header="Refresh server" Click="MenuItem_Click" />
                <MenuItem Name="RefreshAllMenuItem" Header="Refresh all" Click="MenuItem_Click" />
                <MenuItem Name="UpdateServerMenuItem" Header="Update server" Click="MenuItem_Click" />
                <MenuItem Name="UpdateAllMenuItem" Header="Update all" Click="MenuItem_Click" />
                <Separator />
                <MenuItem Name="CloseMenuItem" Header="Afsluiten" Click="MenuItem_Click" />
            </MenuItem>
            <MenuItem Name="ToolsMenuItem" Header="Tools">
                <MenuItem Name="ManageServersMenuItem" Header="Refresh server" Click="MenuItem_Click" />
                <MenuItem Name="ManageDirectoryTypesMenuItem" Header="Refresh all" Click="MenuItem_Click" />
                <MenuItem Name="ManageSourceMenuItem" Header="Update server" Click="MenuItem_Click" />
            </MenuItem>
        </Menu>
        <DockPanel LastChildFill="True">
            <ToolBarPanel DockPanel.Dock="Top">
                <ToolBar />
            </ToolBarPanel>
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Name="Left" Width="2*" ></ColumnDefinition>
                    <ColumnDefinition Name="Middle" Width="3*"></ColumnDefinition>
                    <ColumnDefinition Name="Right" Width="1*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <ListView Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="ServerListView" VerticalAlignment="Stretch" Width="Auto" ItemsSource="{Binding Servers}" Grid.Column="0">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Icoontje" DisplayMemberBinding="{Binding ID}"/>
                            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Name}"/>
                        </GridView>
                    </ListView.View>
                    <!--<ListView.Resources>
                <DataTemplate DataType="{x:Type local:ServerVM}" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="icoontje" />
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text="status" />
                    </StackPanel>
                </DataTemplate>
            </ListView.Resources>-->
                </ListView>
                <TreeView Grid.Column="1" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                </TreeView>
                <TreeView Grid.Column="2" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                </TreeView>
                <GridSplitter Grid.Column="1" Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Name="GridSplitter1" VerticalAlignment="Stretch" Width="1" />
                <GridSplitter Grid.Column="2" Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Name="GridSplitter2" VerticalAlignment="Stretch" Width="1" />
            </Grid>
        </DockPanel>
    </DockPanel>
</Window>

Open in new window

Commented:
The: SizeToContent="WidthAndHeight" resizes the window to the smallest size it can be to fit it's current content. This is why it ignores the Width and Height you gave it.

If you remove this property then the window will be opened with Width="525" Height="248"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial