如何设置 UIElement 的 DesiredSize.Width?
我需要计算自定义面板中每个 UIElement
的 Width
。如果循环 Children
:
foreach (UIElement child in Children) { ... }
由于 DesiredSize
和 DesiredSize.Width
,我无法执行 child.DesiredSize.Width = myCalculatedWidth;
代码> 是只读的。
文档说 Measure()
更新了 UIElement
的 DesiredSize
,所以我尝试了:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果直接调用
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 likeMeasureIsValid
). You should callInvalidateMeasure()
for this control (which will mark control asMeasureIsValid = false)
andUpdateLayout()
then to force layout to be updated.只需尝试使用
child.Width = myCalculatedWidth
即可。Just try with
child.Width = myCalculatedWidth
.调用InvalidateArrange()。渲染系统将调用
Arrange(Rect)
并用您的尺寸更新 UIElement 的DesiredSize
。Call
InvalidateArrange()
. The rendering system will callArrange(Rect)
and update theDesiredSize
of the UIElement with your size.其他答案都不适合我。我需要做的是在子控件中实现 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.无法直接更改 DesiredSize,因为只有 UIElement 可以写入 DesiredSize,这发生在
UIElement.Measure()。然而,Measure()并不计算DesiredWidth,而是调用MeasurementCore。但即使MeasurementCore也不会直接计算DesiredSize,而是使用MeasurementOverride的返回值。这就是您必须重写MeasurementOverride() 来更改DesiredChange 的原因:-)
它看起来像这样:
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:
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.