Stackpanel:高度 vs ActualHeight vs ExtentHeight vs ViewportHeight vs DesiredSize vs RenderSize
我想知道我的 StackPanel
所有项目的高度。
两者之间有什么区别:
高度
- 获取或设置元素的建议高度。ActualHeight
- 获取渲染高度这个元素。 (只读)ExtentHeight
- 获取包含范围垂直尺寸的值。 (只读)
ViewportHeight
- 获取包含内容视口的垂直尺寸的值。 (只读)
DesiredSize
- 获取该元素在布局过程的测量过程中计算的大小。 (只读)RenderSize
- 获取(或设置,但请参阅备注)此元素的最终渲染大小。
来自 MSDN:
高度
获取或设置元素的建议高度。属性值:
Double
- 元素的高度,采用与设备无关的单位(每个单位 1/96 英寸)。元素的高度,采用与设备无关的单位(每单位 1/96 英寸)。
实际高度(只读)
获取此元素的渲染高度。属性值:
Double
- 元素的高度,以与设备无关的单位表示的值(每单位 1/96 英寸)。此属性是基于其他高度输入和布局系统的计算值。该值是由布局系统本身根据实际渲染通道设置的,因此可能会稍微落后于属性的设置值,例如 高度,这是输入更改的基础。
由于 ActualHeight 是一个计算值,因此您应该注意,由于布局系统的各种操作,可能会对其进行多次或增量报告的更改。布局系统可能正在计算子元素所需的度量空间、父元素的约束等等。
范围高度 ( 只读)
获取包含范围的垂直大小的值。属性高度:
Double
- 表示范围垂直大小的 Double。返回的值在设备独立像素中进行了描述。
ViewportHeight ( 只读)
获取一个包含内容视口的垂直尺寸的值。属性值:
Double
- 表示内容视口的垂直尺寸的 Double。返回的值在设备独立像素中进行了描述。
所需大小(只读)
获取该元素在布局过程的测量过程中计算的大小。属性值:
Size
- 计算出的大小,它将成为排列通道所需的大小。仅当 IsMeasureValid 属性的值为 true 时,此属性返回的值才是有效的测量值。
当您实现布局行为覆盖(例如 ArrangeOverride、MeasureOverride 或 OnRender)时,通常会检查 DesiredSize 作为测量因素之一(在 OnRender 情况下,您可能会检查 RenderSize,但这取决于您的实现)。根据具体情况,您的实现逻辑可能会完全遵守 DesiredSize,可能会应用对 DesiredSize 的约束,并且此类约束还可能会更改父元素或子元素的其他特征。例如,支持可滚动区域的控件(但选择不从已启用可滚动区域的 WPF 框架级控件派生)可以将可用大小与 DesiredSize 进行比较。然后,该控件可以设置一个内部状态,以在该控件的 UI 中启用滚动条。或者,在某些情况下,DesiredSize 也可能被忽略。
渲染大小 获取该元素的最终渲染大小。
属性值:
Size
- 此元素的渲染大小。此属性可用于检查布局系统覆盖(例如 OnRender 或 GetLayoutClip)中适用的渲染大小。
更常见的场景是使用类处理程序重写或 OnRenderSizeChanged 事件处理 SizeChanged 事件。
就我而言,我想知道 StackPanel
中所有项目的所需高度。
换句话说:我想知道 StackPanel 中所有项目的高度(在绘制之前),如果它们溢出面板,我将
- 删除
- 缩小
- 比例
- 调整
项目以确保它们适合 StackPanel 。
这意味着我可能想在调整大小事件期间获得所需高度(ExtentHeight?DesiredSize?)(大小更改?LayoutUpdated ) - 在任何绘图发生之前(因此速度更快)。
大多数这些属性返回零;显然,我不知道这些属性的含义,也没有在文档中进行解释。
i want to know the height of all items my StackPanel
.
What is the difference between:
Height
- Gets or sets the suggested height of the element.ActualHeight
- Gets the rendered height of this element. (readonly)ExtentHeight
- Gets a value that contains the vertical size of the extent. (readonly)ViewportHeight
- Gets a value that contains the vertical size of the content's viewport. (readonly)DesiredSize
- Gets the size that this element computed during the measure pass of the layout process. (readonly)RenderSize
- Gets (or sets, but see Remarks) the final render size of this element.
From MSDN:
Height
Gets or sets the suggested height of the element.Property value:
Double
- The height of the element, in device-independent units (1/96th inch per unit).The height of the element, in device-independent units (1/96th inch per unit).
ActualHeight (readonly)
Gets the rendered height of this element.Property value:
Double
- The element's height, as a value in device-independent units (1/96th inch per unit).This property is a calculated value based on other height inputs, and the layout system. The value is set by the layout system itself, based on an actual rendering pass, and may therefore lag slightly behind the set value of properties such as Height that are the basis of the input change.
Because ActualHeight is a calculated value, you should be aware that there could be multiple or incremental reported changes to it as a result of various operations by the layout system. The layout system may be calculating required measure space for child elements, constraints by the parent element, and so on.
ExtentHeight (readonly)
Gets a value that contains the vertical size of the extent.Property height:
Double
- A Double that represents the vertical size of the extent.The returned value is described in Device Independent Pixels.
ViewportHeight (readonly)
Gets a value that contains the vertical size of the content's viewport.Property value:
Double
- The Double that represents the vertical size of the content's viewport.The returned value is described in Device Independent Pixels.
DesiredSize (readonly)
Gets the size that this element computed during the measure pass of the layout process.Property value:
Size
- The computed size, which becomes the desired size for the arrange pass.The value returned by this property will only be a valid measurement if the value of the IsMeasureValid property is true.
DesiredSize is typically checked as one of the measurement factors when you implement layout behavior overrides such as ArrangeOverride, MeasureOverride, or OnRender (in the OnRender case, you might check RenderSize instead, but this depends on your implementation). Depending on the scenario, DesiredSize might be fully respected by your implementation logic, constraints on DesiredSize might be applied, and such constraints might also change other characteristics of either the parent element or child element. For example, a control that supports scrollable regions (but chooses not to derive from the WPF framework-level controls that already enable scrollable regions) could compare available size to DesiredSize. The control could then set an internal state that enabled scrollbars in the UI for that control. Or, DesiredSize could potentially also be ignored in certain scenarios.
RenderSize
Gets the final render size of this element.Property value:
Size
- The rendered size for this element.This property can be used for checking the applicable render size within layout system overrides such as OnRender or GetLayoutClip.
A more common scenario is handling the SizeChanged event with the class handler override or the OnRenderSizeChanged event.
In my case i want know the desired height of all items in the StackPanel
.
In other words: i want to know height of all items in the StackPanel (before drawing), and if they were to overflow the panel, i will
- delete
- shrink
- scale
- adjust
items to ensure they fit in the StackPanel.
Which means i probably want to get the desired height (ExtentHeight? DesiredSize?) during a resize event (SizeChanged? LayoutUpdated? ) - before any drawing happens (so it's faster).
Most of these properties return zero; so obviously there's some understanding of what these properties mean that i don't know and are not explained in the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如您所知,
StackPanel
是一个 [Panel] 对象。每个面板通过两种方法与其子面板进行通信,以确定最终的尺寸和位置。第一个方法是
MeasureOverride
,第二个方法是ArrangeOverride
。MeasureOveride
询问每个子项所需的大小以及给定的可用空间量。ArrangeOverride
在测量完成后排列子项。让我们创建一个堆栈面板:
DesiredSize(大小
由
MeasureOverride
返回)是总和子项大小的方向
StackPanel 和最大的尺寸
另一个方向的孩子。
RenderSize
代表最终的布局后
StackPanel
的大小完成。
ActualHeight
与RenderSize.Height
。要依赖这些属性,您应该仅在 LayoutUpdated 事件。
ViewPortHeight
外观此处
ExtentHeight
看看这里
As you know the
StackPanel
is a [Panel] object. Each panel communicates with its children by two methods to determine the final sizes and positions.The first method is
MeasureOverride
and the second isArrangeOverride
.The
MeasureOveride
asks each child for desired size with a given amount of space available.ArrangeOverride
arranges the children after measurement has been completed.Let's create a stackpanel:
The
DesiredSize
(the sizereturned by
MeasureOverride
) is sumof child sizes in the direction of
StackPanel and the size of largest
child in the other direction.
RenderSize
represents the finalsize of the
StackPanel
after layoutis complete.
ActualHeight
is exactly same asRenderSize.Height
.For rely these properties you should access them only within an event handler for LayoutUpdated event.
ViewPortHeight
lookhere
ExtentHeight
lookhere
上面的答案是正确的,除了 RenderSize 和 ActualHeight 可以具有暂时不同的值。 RenderSize 在 OnRender 之前设置,而 ActualHeight 在 WPF 完成该控件的布局和渲染处理后设置。最后,LayoutUpdated 被引发。
因此,RenderSize 可以在 OnRender 中使用,但 ActualHeight 仍将具有布局开始之前的旧值。
该序列如下所示:
WPF 可能会多次执行该序列(递归)。一旦一切都解决了,就会执行以下操作:
在第一个布局完成后,可以随时访问 ActualHeight(!),除了在测量、排列和渲染的布局过程本身期间。 WPF 确保所有代码在布局处理运行之前完成。
Above answer is correct, except that RenderSize and ActualHeight can have temporarily different values. RenderSize gets set before OnRender, whereas ActualHeight gets set once WPF has finished the layout and render processing for that control. At the very end, LayoutUpdated gets raised.
Therefore, RenderSize can be used within OnRender, but ActualHeight will still have the old value from before the layout started.
The sequence looks like this:
WPF might execute this sequence several times (recursion). Once everything is settled, the following gets executed:
ActualHeight can be accessed any time (!) after the first layout is done, except during the layout process itself of measure, arrange and render. WPF ensures that any code gets completed before layout processing runs.