2013-03-11 37 views
2

這應該很簡單:我如何在DockPanel中拉伸StackPanel,以便填充整個父級的內容並保持其Horizo​​ntalAlignment?在DockPanel中填充StackPanel

實施例:

<DockPanel LastChildFill="True"> 
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="Yellow"> 
     <Button Height="30">Button1</Button> 
     <Button Height="30">Button2</Button> 
    </StackPanel> 
</DockPanel> 

在這個例子中的StackPanel的寬度是相同的,從兩個按鈕的組合寬度(和這裏的背景是黃色),但在DockPanel中的剩餘空間保持白色。看起來LastChildFille屬性在我的示例中不起作用。

回答

2

我認爲通過在StackPanel上設置HorizontalAligmentCenter,您重寫了DockPanel的行爲來使用StackPanel填充整個空間。

當我明白你的問題的權利,你想要的全部空間是黃色的和按鈕的堆疊面板居中在中間。然後你應該將StackPanel包裝在一個Grid中。

<DockPanel LastChildFill="True"> 
    <Grid Background="Yellow"> 
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
     <Button Height="30">Button1</Button> 
     <Button Height="30">Button2</Button> 
    </StackPanel> 
    </Grid> 
</DockPanel> 
+0

絕對正確。另一種選擇是在按鈕上設置「Horizo​​ntalAlignment」,不過它們不一定是相同的寬度,這可能是不合需要的。 – 2013-03-11 08:37:53

+0

在這種情況下,我可以將背景屬性設置爲DockPanel。我的意圖是爲StackPanel創建背景風格,但這也應該工作... – sventevit 2013-03-11 09:04:03