Window Height="Auto" not working as expected

What I am trying to do is show a window, that does not explicitly have a height/width, (both values omitted or set to Auto). I was guessing that the window would find out its size by auto - calculating all contained usercontrols sizes, but this doesn't actually work!

Instead I get a big window with Actualwidth and Actualheight values both set to 512 (?!?!)

Window declaration:

<Window x:Class="Window3"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window3" 
  Height="Auto">
<StackPanel>
    <Label>Window</Label>
</StackPanel>
</Window>

Showing this window as a dialog via:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
    Dim dlg As New Window3
    dlg.ShowDialog()
End Sub

Is there a solution for this? I don't want to explicitly set the size of my window because many controls in the form will be collapsed based on constructor parameters, and trying to find the actual size of the form would be tricky (and ugly).


Set the window's property SizeToContent="WidthAndHeight". This should help.


Old question but an updated answer:

As @Muad'Dib suggested in his answer :

you should set SizeToContent="WidthAndHeight".

If the size of content of window increases more than the screen size, window can overflow from screen. So, you must consider:

MaxWidth="600"
MaxHeight="400"

Well you can't set the window height to auto, to do this you can use a little trick, name the main main grid container, set its height to auto then bind the window height to the height of the main grid