Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a Silverlight container to expand/contract to the size of its child controls?

Tags:

silverlight

I've got a root UserControl that is 300 high.

Inside that I have a Border that I want to expand to the size of its own controls, so if I stack in more controls, it will expand -- less controls, it will contract.

However when I set it to "Auto" it expands it out to the size of its parent container instead of the size of its child controls.

How can I get Border to expand and contract to the size of its child controls, something like the functionality of an HTML table?

<UserControl   x:Class="Second105.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Border 
            Background="Tan" 
            CornerRadius="10" 
            Padding="10"
            Width="300" 
            Height="Auto">
        <StackPanel>
                <TextBlock HorizontalAlignment="Center" Margin="0 0 0 5">Please select a <Run FontStyle="Italic">week day</Run>:</TextBlock>
            <basics:Calendar
                Name="theCalendar" 
                SelectedDatesChanged="Calendar_SelectedDatesChanged"/>
            <TextBlock
                Name="theMessage"
                Margin="0 10 0 0"
                HorizontalAlignment="Center"
                Text="..."/>
        </StackPanel>
        </Border>
    </Grid>
</UserControl>
like image 496
Edward Tanguay Avatar asked Feb 13 '09 09:02

Edward Tanguay


1 Answers

Wrap it in a StackPanel should do it:

<UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Width="400"
   Height="300">
   <Grid x:Name="LayoutRoot" Background="White">
      <StackPanel>
         <Border
            Width="300"
            Height="Auto"
            Background="Tan"
            CornerRadius="10"
            Padding="10">
            <StackPanel>
               <TextBlock HorizontalAlignment="Center" Margin="0 0 0 5">Please select a 
                  <Run FontStyle="Italic">week day
                  </Run>:
               </TextBlock>
               <TextBlock
                  Name="theMessage"
                  HorizontalAlignment="Center"
                  Margin="0 10 0 0"
                  Text="..."/>
            </StackPanel>
         </Border>
      </StackPanel>
   </Grid>
</UserControl>
like image 162
Steven Robbins Avatar answered Nov 03 '22 00:11

Steven Robbins