Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smailliwcs/f7509fd3f38632a3a88ce4f48b2eb26c to your computer and use it in GitHub Desktop.
Save smailliwcs/f7509fd3f38632a3a88ce4f48b2eb26c to your computer and use it in GitHub Desktop.
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment