Stackpanel: Height vs ActualHeight vs ExtentHeight vs ViewportHeight vs DesiredSize vs RenderSize

前端 未结 2 958
无人及你
无人及你 2021-01-31 09:57

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 eleme
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 10:15

    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 is ArrangeOverride.

    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:

    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;
        }
    }
    
    • The DesiredSize (the size returned by MeasureOverride) is sum of child sizes in the direction of StackPanel and the size of largest child in the other direction.

    • RenderSize represents the final size of the StackPanel after layout is complete.

    • ActualHeight is exactly same as RenderSize.Height.

    For rely these properties you should access them only within an event handler for LayoutUpdated event.

    • for ViewPortHeight look here
    • for ExtentHeight look here

提交回复
热议问题