我有一个高为300的根UserControl。
在其中,我有一个Border,我想扩展到其自己的控件的大小,因此,如果我堆叠更多控件,它会扩展-更少的控件,它将收缩。
但是,当我将其设置为“自动”时,它会将其扩展到其父容器的大小,而不是其子控件的大小。
如何使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>
最佳答案
将其包装在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>
关于silverlight - 如何强制Silverlight容器扩展/收缩到其子控件的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/545400/