如何设置 UIElement 的 DesiredSize.Width?

发布于 2024-08-12 10:37:34 字数 621 浏览 9 评论 0原文

我需要计算自定义面板中每个 UIElementWidth 。如果循环 Children

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

由于 DesiredSizeDesiredSize.Width,我无法执行 child.DesiredSize.Width = myCalculatedWidth;代码> 是只读的。

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

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

但是 child.DesiredSize.Width< /code> 不变。如何设置DesiredSize.Width

I need to calculate the Width of each UIElement in a custom panel. If looping over Children:

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

I can't do child.DesiredSize.Width = myCalculatedWidth; since DesiredSize and DesiredSize.Width are read only.

The docs say that that Measure() updates the DesiredSize of the UIElement, so I tried:

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

But child.DesiredSize.Width is unchanged. How do I set the DesiredSize.Width?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心欲静而疯不止 2024-08-19 10:37:34

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

If you call Measure directly there are nothing to do for WPF layout system (because it caches the state of controls and store it as flags like MeasureIsValid). You should call InvalidateMeasure() for this control (which will mark control as MeasureIsValid = false) and UpdateLayout() then to force layout to be updated.

温柔女人霸气范 2024-08-19 10:37:34

只需尝试使用 child.Width = myCalculatedWidth 即可。

Just try with child.Width = myCalculatedWidth.

明明#如月 2024-08-19 10:37:34

调用InvalidateArrange()。渲染系统将调用 Arrange(Rect) 并用您的尺寸更新 UIElement 的 DesiredSize

Call InvalidateArrange(). The rendering system will call Arrange(Rect) and update the DesiredSize of the UIElement with your size.

我做我的改变 2024-08-19 10:37:34

其他答案都不适合我。我需要做的是在子控件中实现 MeasureOverride 并在那里实现所需的布局行为。

None of the other answers worked for me. What I needed to do was to implement MeasureOverride in the child control and implement the desired layout behavior there.

玩物 2024-08-19 10:37:34

无法直接更改 DesiredSize,因为只有 UIElement 可以写入 DesiredSize,这发生在
UIElement.Measure()。然而,Measure()并不计算DesiredWidth,而是调用MeasurementCore。但即使MeasurementCore也不会直接计算DesiredSize,而是使用MeasurementOverride的返回值。这就是您必须重写MeasurementOverride() 来更改DesiredChange 的原因:-)

它看起来像这样:

UIElement.Measure() calls 
  FrameworkElement.MeasureCore() calls
    YourControl.MeasureOverwrite()

MeasureOverwrite() 的返回值变成MeasureCore() 的返回值,然后存储在UIElement.DesiredSize 中。

请注意,MeasureOverwrite() 返回的值不一定与 DesiredSize 中存储的值相同,因为 Margin 会添加到返回值中! FrameworkElement.MeasurementCore() 还确保 DesiredSize 分别在 MinWidth、MaxWidth 和 Height 范围内。如果返回值的 Width 或 Height isInfinite 或 isNaN,则会引发异常。

One cannot change directly DesiredSize, since only UIElement can write to DesiredSize, which happens in
UIElement.Measure(). However, Measure() does not calculate DesiredWidth, but calls MeasurementCore instead. But even MeasurementCore does not calculate DesiredSize directly, but uses the return value of MeasurementOverride instead. That's the reason you have to override MeasurementOverride() to change DesiredChange :-)

It looks like this:

UIElement.Measure() calls 
  FrameworkElement.MeasureCore() calls
    YourControl.MeasureOverwrite()

return value of MeasureOverwrite() becomes return value of MeasureCore(), which then is stored in UIElement.DesiredSize.

Note that the value returned by MeasureOverwrite() is not necessarily the same as stored in DesiredSize, because the Margin gets added to the returned value ! FrameworkElement.MeasurementCore() also ensures that DesiredSize is within MinWidth, MaxWidth and Height, respectively. If the Width or Height of the returned value isInfinite of isNaN, an exception gets thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文