tags:

views:

55

answers:

3

I have a wpf application with a few forms. At design time they are small, and they are not set to auto size. However at run time they are giant, even with no content in them to make them big.

Why is this happening?

Here is one of the forms

<Window x:Class="SuperPluginPicker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:tree="clr-namespace:Aga.Controls.Tree;assembly=Aga.Controls" 
             mc:Ignorable="d" 
             d:DesignHeight="296" d:DesignWidth="634" Title="Plugin Selector" WindowStartupLocation="CenterOwner">
    <Grid>
        <DockPanel LastChildFill="true">
            <StackPanel DockPanel.Dock="Bottom" Height="30" Orientation="Horizontal">
                <Button Content="Ok" Name="btnOk" Click="btnOk_Click"></Button>
                <Button Content="Cancel" Name="btnCancel" Click="btnCancel_Click"></Button>

            </StackPanel>
            <StackPanel DockPanel.Dock="Right">
                <Label Content="Selected Plugins"></Label>
            <ListBox Name="lstSelectedPlugins"  Width="200">
                <ListBox.ItemTemplate>



                    <DataTemplate>



                        <Label Content="{Binding Name}" />

                    </DataTemplate>

                </ListBox.ItemTemplate>

            </ListBox>
            </StackPanel>
            <StackPanel DockPanel.Dock="Right" Width="100" VerticalAlignment="Center">
                <Button Content="Add" Name ="btnAdd" Click="btnAdd_Click"></Button>
                <Button Content="Remove" Name="btnRemove" Click="btnRemove_Click"></Button>
                <Button Content="Remove All" Name="btnRemoveAll" Click="btnRemoveAll_Click"></Button>
            </StackPanel>

            <tree:TreeList  x:Name="pluginTree">
                <tree:TreeList.View>
                    <GridView x:Name="treeGrid">
                        <GridView.Columns>

                            <GridViewColumn Width="Auto" Header="Name">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <tree:RowExpander/>

                                            <TextBlock Text="{Binding Name}"></TextBlock>
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Author" Width="Auto"  DisplayMemberBinding="{Binding Author}"/>
                            <GridViewColumn Header="Description" Width="Auto" DisplayMemberBinding="{Binding Type}"/>


                        </GridView.Columns>
                    </GridView>
                </tree:TreeList.View>
            </tree:TreeList>
        </DockPanel>
    </Grid>
</Window>
A: 

Why don't you just add a:

Height="296" Width="634"

to the Window definition? I'm not sure where the default size comes from but I've set specific sizes on my windows to get around this. Then I dynamically resize in the application startup code.

I'm hoping that WPF will have a pack-like feature where it recursively sets controls based on the preferred sizes of the sub-controls (Swing and wxPython both had this feature) so you may want to go looking for that.


Actually, a bit of searching has turned up the Window.SizeToContent property which may be what you're looking for. Its default is Manual but, if you set it to one of the other values (Width, Height or WidthAndHeight), you may see a better presentation.

In fact, I just nicked over to the Windows laptop and entered:

SizeToContent="WidthAndHeight"

into the Window definition and it looks perfectly sized. So, I'd suggest giving that a try and seeing if it fixes (or at least works around) your problem.

paxdiablo
That works, but why is it picking such a huge default size?
Jason Coyne
@Jason: no idea, but I suspect it's querying your system metrics to get the screen size. My current app (my first WPF one) behaves similarly in that it centers and leaves a little around the edges. I remember reading somewhere that WPF would default to 60% of the width and height of the primary monitor if you didn't specify it but also that there were _many_ other things that could also influence it (though I can't find that reference now). I think I'll just go with the `SizeToContent` in future (see my update) and not rely on some MS flunkie guessing right :-)
paxdiablo
And I found the link finally, which just happens to make this question a dupe unfortunately: http://stackoverflow.com/questions/1437099/window-size-when-sizetocontent-is-not-specified
paxdiablo
Im ok with it being a duplciate as long as I get a good answer :)
Jason Coyne
It's not precisely a duplicate question, but its answers do cover the same ground.
Rob Perkins
+4  A: 

In design you are seeing small because you have blend design properties for the window.

d:DesignHeight="296" d:DesignWidth="634"

Since Grid is a dynamic layout it will be stretch to the window's width & height. To restrict you window size you need to mention Height & Width properties to the window.

Ragunathan
+3  A: 

If you create a <Window/> with this code:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

Then when you run that from Visual Studio it will display a window the same size as the one you put in your question. So this isn't anything you explicitly did.

What you've encountered is the default computed size of a WPF Window. For different results, specify a SizeToContent parameter on the Window, as paxdiablo pointed out, or explicitly set the size of your window with the MinHeight, MaxHeight, Height, MinWidth, MaxWidth, and/or Width properties.

Rob Perkins
A very clear, well-written and accurate answer. +1.
Ray Burns
By the way, if you create a window in MFC, Win32 or Win16 you will discover you get the same default size. For example, the Win32 code `CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);` gives you the same size window as WPF does. WPF is not selecting the default size: Windows is doing that itself, using an algorithm that has remained pretty much the same since Windows 2.0 came out in 1987.
Ray Burns
That's computed from the size of the primary screen, then, right?
Rob Perkins