Created
September 22, 2020 15:25
-
-
Save smailliwcs/f7509fd3f38632a3a88ce4f48b2eb26c to your computer and use it in GitHub Desktop.
Automatic WPF grids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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