wpf - Stackpanel:高度 vs ActualHeight vs ExtentHeight vs ViewportHeight vs DesiredSize vs RenderSize

标签 wpf layout height scaling stackpanel

我想知道我的 StackPanel 所有项目的高度。

有什么区别:

  • Height - 获取或设置元素的建议高度。
  • ActualHeight - 获取该元素的渲染高度。 (只读)
  • ExtentHeight - 获取包含范围垂直大小的值。 (只读)
  • ViewportHeight - 获取包含内容视口(viewport)的垂直尺寸的值。 (只读)
  • DesiredSize - 获取该元素在布局过程的测量过程中计算的大小。 (只读)
  • RenderSize - 获取(或设置,但请参阅备注)此元素的最终渲染大小。
<小时/>

来自 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.

<小时/>

就我而言,我想知道 StackPanel 中所有项目的所需高度。

换句话说:我想知道 StackPanel 中所有项目的高度(在绘制之前),如果它们溢出面板,我会

  • 删除
  • 收缩
  • 规模
  • 调整

确保它们适合StackPanel的项目。

这意味着我可能希望在调整大小事件期间获得所需高度(ExtentHeight?DesiredSize?)(SizeChangedLayoutUpdated?) - 在任何事件之前绘图发生(因此速度更快)。

这些属性中的大多数返回零;显然,我不知道这些属性的含义,也没有在文档中进行解释。

最佳答案

如您所知,StackPanel 是一个 [Panel] 对象。每个面板通过两种方法与其子面板进行通信,以确定最终的尺寸和位置。 第一个方法是 MeasureOverride,第二个方法是 ArrangeOverride

MeasureOveride 会询问每个子项所需的大小以及给定的可用空间量。 ArrangeOverride 在测量完成后排列子项。

让我们创建一个堆栈面板:

public class AnotherStackPanel : Panel
{
    public static readonly DependencyProperty OrientationProperty =
        DependencyProperty.Register(“Orientation”, typeof(Orientation),
        typeof(SimpleStackPanel), new FrameworkPropertyMetadata(
        Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsMeasure));

    public Orientation Orientation
    {
        get { return (Orientation)GetValue(OrientationProperty); }
        set { SetValue(OrientationProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        Size desiredSize = new Size();

        if (Orientation == Orientation.Vertical)
            availableSize.Height = Double.PositiveInfinity;
        else
            availableSize.Width = Double.PositiveInfinity;
        foreach (UIElement child in this.Children)
        {
            if (child != null)
            {
                child.Measure(availableSize);

                if (Orientation == Orientation.Vertical)
                {
                    desiredSize.Width = Math.Max(desiredSize.Width,
                    child.DesiredSize.Width);
                    desiredSize.Height += child.DesiredSize.Height;
                }
                else
                {
                    desiredSize.Height = Math.Max(desiredSize.Height,
                    child.DesiredSize.Height);
                    desiredSize.Width += child.DesiredSize.Width;
                }
            }
        }
        return desiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        double offset = 0;
        foreach (UIElement child in this.Children)
        {
            if (child != null)
            {
                if (Orientation == Orientation.Vertical)
                {               
                    child.Arrange(new Rect(0, offset, finalSize.Width,
                    child.DesiredSize.Height));                 
                    offset += child.DesiredSize.Height;
                }
                else
                {                   
                    child.Arrange(new Rect(offset, 0, child.DesiredSize.Width,
                    finalSize.Height));
                    offset += child.DesiredSize.Width;
                }
            }
        }
        return finalSize;
    }
}
  • DesiredSize(大小 由 MeasureOverride 返回)是总和 子项大小的方向 StackPanel 和最大的尺寸 child 在另一个方向。

  • RenderSize代表最终的 布局后 StackPanel 的大小 已完成。

  • ActualHeightRenderSize.Height

要依赖这些属性,您应该仅在 LayoutUpdated 的事件处理程序中访问它们。事件。

关于wpf - Stackpanel:高度 vs ActualHeight vs ExtentHeight vs ViewportHeight vs DesiredSize vs RenderSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6403091/

相关文章:

c# - 处理来自消息框的回复 C#

wpf - 生产质量 W​​PF 示例

wpf - 为什么我的命令绑定(bind)没有触发?

javascript - Packery 网格布局 - 边框/填充和覆盖问题

php - 相对 Div 内的绝对 Div 的响应高度

c# - 验证 ItemsControl 中的项目

视觉( block 回流)行的第一个元素的 CSS 选择器

java - 使relativeLayout的一部分可滚动?

javascript - 获取可变 div 高度并将 var 放入另一个 css 选择器中

c# - WPF 从底部到顶部调整窗口大小