gpt4 book ai didi

wpf - 我怎样才能得到堆栈面板的实际当前高度?

转载 作者:行者123 更新时间:2023-12-04 00:48:31 26 4
gpt4 key购买 nike

你好,

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

最佳答案

这可能与您实际查询 this.ActualHeight时间有关。在调用 AddChild() 时,高度可能仍为 0,因为 the measure pass and the arrange pass可能还没有通过。所有影响 child 尺寸和布局的事情都应该在MeasureOverride中完成和 ArrangeOverride

您应该看看如何编写自定义面板。那里有大量关于这个主题的资源。我个人认为this是一个很好且足够简单的教程。

关于wpf - 我怎样才能得到堆栈面板的实际当前高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3683833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com