有没有一个WPF“WrapGrid”控制可用还是创建一个简单的方法? [英] Is there a WPF "WrapGrid" control available or an easy way to create one?

查看:101
本文介绍了有没有一个WPF“WrapGrid”控制可用还是创建一个简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要一个wrapPanel,但我希望项目能够对齐到一个网格,而不是被按到左边,这样我就可以获得一个很好的统一外观网格,它会自动消耗可用空间。



WrapPanel处理调整大小的部分。
WPF.Contrib.AutoGrid处理一个很好的自动网格。



任何人都有一个控制它的结合?



我的使用案例是我有一系列有些不规则形状的控件。我希望它们出现在漂亮的列中,这样在放置控件时,换行面板应该对齐下一个tabstop。 解决方案

以下是一些基于其他一些接近控件的代码。它在布局方面做得不错,尽管它存在一个问题,那就是盛大的子控件没有填满所有可用的空间。

  protected override Size ArrangeOverride(Size finalSize)
{
double rowY = 0;
int col = 0;
double currentRowHeight = 0;


foreach(UIElement child in Children)
{
var initialSize = child.DesiredSize;
int colspan =(int)Math.Ceiling(initialSize.Width / ColumnSize);
Console.WriteLine(colspan);
double width = colspan * ColumnSize;如果(col> 0&&(col * ColumnSize)+ width> constrainedSize.Width)




$ b {
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;
}

尺寸constrainedSize;

保护覆盖尺寸MeasureOverride(尺寸约束)
{
constrainedSize =约束;
返回base.MeasureOverride(约束);
}


Essentially I want a wrapPanel, but I would like items to snap to a grid rather than be pressed up to the left, so I can get a nice uniform looking grid, that automatically consumes available space.

WrapPanel handles the resize part. WPF.Contrib.AutoGrid handles a nice automatic grid.

Anyone got a control that combines them?

My use case is I have a series of somewhat irregularly shaped controls. I would like them to appear in nice columns so the wrap panel should snap to the next "tabstop" when placing a control

解决方案

Here is some code that I whipped up based on some of the other controls that are close. It does a decent job of doing the layout, although it has an issue where grand-child controls do not fill up all their available space.

  protected override Size ArrangeOverride(Size finalSize)
    {
        double rowY = 0;
        int col = 0;
        double currentRowHeight = 0;


        foreach (UIElement child in Children)
        {
            var initialSize = child.DesiredSize;
            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);
    }

这篇关于有没有一个WPF“WrapGrid”控制可用还是创建一个简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆