Making a Viewbox scale vertically but stretch horizontally

后端 未结 4 1532
南旧
南旧 2020-12-03 01:36

I want to make a Viewbox (or something similar) that scales only its height, and then stretches its content horizontally.

If I do this:

         


        
4条回答
  •  半阙折子戏
    2020-12-03 02:16

    To keep width to work correctly:

    protected override Size MeasureOverride(Size availableSize)
        {
            double height = 0;
            Size unlimitedSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
            foreach (UIElement child in Children)
            {
                child.Measure(unlimitedSize);
                height += child.DesiredSize.Height;
            }
            scale = availableSize.Height / height;
    
            foreach (UIElement child in Children)
            {
                unlimitedSize.Width = availableSize.Width / scale;
                child.Measure(unlimitedSize);
            }           
    
            return availableSize;
        }
    

提交回复
热议问题