Automatic WPF Grids

using System.Windows;
using System.Windows.Controls;

public class AutoGrid : Grid
{
    protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
    {
        base.OnVisualChildrenChanged(visualAdded, visualRemoved);
        SetCells();
    }

    private void SetCells()
    {
        int columnCount = ColumnDefinitions.Count;
        for (int index = 0; index < Children.Count; index++)
        {
            UIElement child = Children[index];
            SetRow(child, index / columnCount);
            SetColumn(child, index % columnCount);
        }
    }
}

Leave a Reply

Your email address will not be published.