如何强制Silverlight容器扩展/收缩到其子控件的大小?

时间:2009-02-13 09:55:38

标签: silverlight

我有一个300高的root UserControl。

在里面,我有一个边框,我想扩展到它自己的控件的大小,所以如果我在更多控件中堆叠,它将扩展 - 更少的控件,它将收缩。

但是,当我将其设置为“自动”时,它会将其扩展为其父容器的大小,而不是其子控件的大小

如何让Border扩展和缩小到其子控件的大小,就像HTML表的功能一样?

<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>

1 个答案:

答案 0 :(得分:6)

将它包装在StackPanel中应该这样做:

<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>