用矩形填充面板

时间:2010-06-10 18:03:46

标签: wpf silverlight xaml

我想用矩形填充面板,当面板调整大小时,矩形也应调整大小。

为什么以下不起作用?

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Rectangle Fill="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <Rectangle Fill="Green" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <Rectangle Fill="Blue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
  </StackPanel>
</Page>

我不想使用Grid,因为添加/删除列和重新安排孩子的痛苦。 (我期待StackPanel,因为如果我想在开头添加黄色Rectangle,我只需要声明它。我不需要手动重新订购其他人。)

2 个答案:

答案 0 :(得分:3)

或UniformGrid:

<UniformGrid Columns="1">
    <Rectangle Fill="Red"/>
    <Rectangle Fill="Green"/>
    <Rectangle Fill="Blue"/>
</UniformGrid>

答案 1 :(得分:1)

您正在使用StackPanel,其行为是采用其子级的大小。使用占用整个可用尺寸的Grid

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <Rectangle Fill="Red" Grid.Row="0" />
  <Rectangle Fill="Green" Grid.Row="1" />
  <Rectangle Fill="Blue" Grid.Row="2" />
</Grid>
相关问题