gpt4 book ai didi

wpf - 如何设置 UIElement 的 DesiredSize.Width?

转载 作者:行者123 更新时间:2023-12-01 12:40:05 28 4
gpt4 key购买 nike

我需要计算自定义面板中每个 UIElementWidth。如果遍历 Children:

foreach (UIElement child in Children) { ... }

我不能执行 child.DesiredSize.Width = myCalculatedWidth; 因为 DesiredSizeDesiredSize.Width 是只读的。

文档说 Measure() 更新了 UIElementDesiredSize,所以我尝试了:

Size size = new Size(myCalculatedWidth, Double.PositiveInfinity);
child.Measure(size);

但是 child.DesiredSize.Width 没有改变。如何设置 DesiredSize.Width

最佳答案

如果您直接调用 Measure,则 WPF 布局系统无需执行任何操作(因为它缓存控件的状态并将其存储为标志,如 MeasureIsValid)。您应该为此控件调用 InvalidateMeasure()(它将控件标记为 MeasureIsValid = false)UpdateLayout() 然后强制布局为已更新。

关于wpf - 如何设置 UIElement 的 DesiredSize.Width?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1821335/

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