开发者

Fill panel with rectangles

开发者 https://www.devze.com 2023-01-02 20:51 出处:网络
I want to fill a panel with rectangles, and when the panel is resized, the rectangles should resize as well.

I want to fill a panel with rectangles, and when the panel is resized, the rectangles should resize as well.

Why doesn't the following work?

<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" />
  </St开发者_如何学编程ackPanel>
</Page>

I would prefer not to use a Grid because of the pain of adding/removing columns and rearranging the children. (I was looking forward to StackPanel because if I wanted to add a yellow Rectangle at the beginning, I just declare it. I don't have to re-order the others by hand.)


Or a UniformGrid:

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


You're using a StackPanel whose behavior is to take the size of its children. Use a Grid which takes the whole available size:

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

精彩评论

暂无评论...
验证码 换一张
取 消