views:

98

answers:

4

Hi,

I've got a user control that I'm loading into a Window dynamically - I wanted to set the Window so that it didn't have a size and then I thought the window to resize accordingly depending on the UserControl. However it dosn't - can anyone assist please?

I've made a very basic example - I've cut out the dynamic bits and just put a UserControl in a Window. What do I need to do to get the window to be tight around the UserControl?

Thanks,

Andy

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="LightBlue">
    <Grid>
    </Grid>
</UserControl>


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="Window1"  >
    <Grid>
        <WpfApplication1:UserControl1>
        </WpfApplication1:UserControl1>
    </Grid>
</Window>
+6  A: 

Try setting SizeToContent to WidthAndHeight on your Window.

See MSDN Page

apandit
Works a treat, thank you!
Andy Clarke
A: 

Try setting either Width and Height to Auto, or setting SizeToContent = WidthAndHeight.

Tim Ridgely
A: 

Once you know the size of the control, you will then have to update the size of the window. I'm unsure of any way to force the window to resize itself automatically unless you have code doing it.

Check this out for all you need to know, plus some, in order to do this.

Jaxidian
A: 

Not sure if this will help, but I'd start by making the Window :

Height="Auto" Width="Auto"

If this alone doesn't do the trick I would add a Grid Row and Column

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"  />
</Grid.ColumnDefinitions>

And then I would set the

<WpfApplication1:UserControl1 Grid.Row="0" Grid.Column="0" />

Not 100% sure if this will work but its worth a try as this is what I'm doing on my side and it works.

Richard