Is there a WPF "WrapGrid" control available or an easy way to create one?

Essentially, I want a wrapPanel, but I would like the elements to snap to the grid and not be pressed left, so I can get a beautiful grid with a uniform look that automatically consumes the available space.

WrapPanel handles a portion of the resize. WPF.Contrib.AutoGrid handles a beautiful automatic grid.

Has anyone got a control that combines them?

My use case: I have several elements with irregular shape. I would like them to appear in good columns, so the cover panel should snap to the next tablop when placing the control

+3
source share
3 answers

Here is the code that I whipped based on some other controls that are close. It does a decent job of creating a layout, although it has a problem when grand-child controls do not fill all of their free 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);
    }
0
source

When I read your question, I assumed that you wanted something like this:

public class UniformWrapPanel : WrapPanel
{
  protected override Size MeasureOverride(Size constraint)
  {
    if(Orientation == Orientation.Horizontal)
      ItemWidth = Children.Select(element =>
        {
          element.Measure(constraint);
          return element.DesiredWidth;
        }).Max();
    else
      ... same for vertical ...

    return base.MeasureOverride(constraint);
  }
}

but I see that someone else has already implemented "UniformWrapPanel", and from your comments you indicate that this is not what you were looking for.

A comment I don't understand is:

I want it to not force the elements to be given a size, but use their existing size and therefore automatically determine the width of the columns.

, , , ? . "tabstop", , .

+1

Try setting the ItemWidth (or ItemHeight) WrapPanel property:

   <WrapPanel ItemWidth="48">
    <TextBlock Text="H" Background="Red"/>
    <TextBlock Text="e" Background="Orange"/>
    <TextBlock Text="l" Background="Yellow"/>
    <TextBlock Text="l" Background="Green"/>
    <TextBlock Text="o" Background="Blue"/>
    <TextBlock Text="!" Background="Violet"/>
   </WrapPanel>
-1
source

Source: https://habr.com/ru/post/1749283/


All Articles