2010-06-10 107 views
2

我想用矩形填充面板,當面板調整大小時,矩形也應調整大小。用矩形填充面板

爲什麼以下不工作?

<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,我只是聲明瞭。我不必再爲了別人的手。)

+0

注意StackPanel中不起作用的原因是StackPanel中從來沒有給比自己DesiredSize其子女多在堆疊方向。由於Rectangle的DesiredSize爲零,所有三個矩形都摺疊爲空。 – 2010-06-12 01:32:48

回答

3

還是一個UniformGrid:

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

啊哈!我忘記了UniformGrid的全部內容。這完全符合我想要的。 – moswald 2010-06-10 18:33:33

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

Bleh。有任何解決方案不需要使用網格?我不想在每次決定增加矩形數時增加一個新的RowDefinition。 – moswald 2010-06-10 18:28:22