views:

32

answers:

2

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"&gt;
  <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>

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

A: 

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>
Julien Lebosquain
Bleh. Have any solution that doesn't require the use of a Grid? I don't want to have to add a new RowDefinition every time I decide to increase the number of Rectangles.
mos
+1  A: 

Or a UniformGrid:

<UniformGrid Columns="1">
    <Rectangle Fill="Red"/>
    <Rectangle Fill="Green"/>
    <Rectangle Fill="Blue"/>
</UniformGrid>
Goblin
Aha! I had forgotten all about UniformGrid. That fits perfectly with what I want.
mos