Previous month:
October 2007
Next month:
February 2008

January 2008

Resizing the Main Window of WPF Applications

If the main Window of an WPF application should automatically be sized to its content, setting the Width and Height properties to Auto does not help. With this setting the Window gets a default height and width. To resize the main window to its content, the property SizeToContent can be set. If the Height and Width properties are set as well to Auto, the Window gets resized dynamically as the size of the content changes.

<Window x:Class="SizingTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Width="Auto" Height="Auto" SizeToContent="WidthAndHeight" >
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Label Margin="5" Grid.Column="0" Grid.Row="0">Value 1:</Label>
    <Label Margin="5" Grid.Column="0" Grid.Row="1">Value 2:</Label>
    <Label Margin="5" Grid.Column="0" Grid.Row="2">Value 3:</Label>
    <TextBox Margin="5" Grid.Column="1" Grid.Row="0" MinWidth="100" />
    <TextBox Margin="5" Grid.Column="1" Grid.Row="1" MinWidth="100" />
    <TextBox Margin="5" Grid.Column="1" Grid.Row="2" MinWidth="100" />
  </Grid>
</Window>

Christian