我想在WPF自定義的StackPanel,它應自動調整自己的孩子的,這取決於面板的高度一定規模。但面板高度是動態的,因爲它延伸到他的父母。當我想要得到的高度(我嘗試了所有的可能性,看代碼),它始終是0或者沒有定義,但在構建解決方案,絕對不爲0 下面的代碼:
XAML:
<my:AutoSizeButtonStackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
代碼隱藏:
public class AutoSizeButtonStackPanel : StackPanel
{
public void AddChild(Button newButton)
{
double getPanelHeight;
getPanelHeight = this.ActualHeight; //is 0
getPanelHeight = this.ViewportHeight; //is 0
getPanelHeight = this.Height; //is n. def.
getPanelHeight = this.DesiredSize.Height; //is 0
getPanelHeight = this.RenderSize.Height; //is 0
newButton.Height = getPanelHeight/2;
this.Children.Add(newButton);
}
}
謝謝,很好的教程(即使對我來說足夠簡單:-P) 只需在ArrangeOverride中放置調整大小邏輯,現在它可以工作。 – SpeziFish 2010-09-10 11:43:14