你好,
我想在 WPF 中制作一个自定义堆栈面板,它会根据面板的高度自动将他的孩子调整为一定的大小。但是面板高度是动态的,因为它延伸到他的父母。当我想获得高度时(我尝试了所有可能性,请参阅代码),它始终为 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);
}
}