在包裹面板中水平拉伸

时间:2014-04-24 19:34:58

标签: c# wpf xaml

当分辨率适合时,我将有2列控件,但如果分辨率变得足够小,我会尝试动态调整大小到一列。除了我的两列中的控件都没有在屏幕上伸展之外,一个包装面板非常适用于此。我听说过像创建自定义控件(例如UniformWrapGrid)这样的差异,但没有运气。处理这个问题的最佳方法是什么?感谢任何建议,指出我正确的方向

 <WrapPanel Orientation="Horizontal">

      <Grid x:Name="GridOne"> <!-- My controls (One Column) --> </Grid>

      <Grid x:Name="GridTwo"> <!-- Some more controls (One column) --> </Grid>

 </WrapPanel>

我尝试过:(这适用于第一列,但第二列在新行而不是第一行的第二列水平拉伸)

public class UniformWrapPanel : WrapPanel
{
    protected override Size ArrangeOverride(Size finalSize)
    {
        double rowY = 0;
        int col = 0;
        double currentRowHeight = 0;

        foreach (UIElement child in Children)
        {
            var initialSize = child.DesiredSize;
            double columnSize = constrainedSize.Width / 2;
            int colspan = (int)Math.Ceiling(initialSize.Width / columnSize);
            Console.WriteLine(colspan);
            double width = colspan * columnSize;



            if (col > 0 && (col * columnSize) + width > constrainedSize.Width)
            {
                rowY += currentRowHeight;
                col = 0;
                currentRowHeight = 0;
            }


            var childRect = new Rect(col * columnSize, rowY, width, initialSize.Height);
            child.Arrange(childRect);
            currentRowHeight = Math.Max(currentRowHeight, initialSize.Height);
            col += colspan;
        }

        return finalSize;
    }

    Size constrainedSize;

    protected override Size MeasureOverride(Size constraint)
    {
        constrainedSize = constraint;
        return base.MeasureOverride(constraint);
    }

0 个答案:

没有答案