I got tired of writing out RowDefinitions
and ColumnDefinitions
for my Grids, so created some custom DependencyProperties that let you specify the number of Rows/Columns in the Grid definition.
The code for the Dependency Properties can be found here and are used like this:
<Grid local:GridHelpers.RowCount="{Binding RowCount}"
local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
If it gives you an error about the binding then change the typeof(Grid)
in the DependencyProperty definition to typeof(GridHelpers)
. My first version of the helper class didn't allow bindings and I can't remember which one I have posted.
Edit
Here's the code I am using which works, including correctly updating the UI when SomeInt
changes. I was testing with switching SomeInt
between 2 and 3 when clicking a button
XAML
<Grid ShowGridLines="True"
local:GridProperties.ColumnCount="{Binding SomeInt}"
local:GridProperties.RowCount="{Binding SomeInt}">
<TextBox Text="Test" Grid.Row="0" Grid.Column="0" />
<TextBox Text="Test" Grid.Row="0" Grid.Column="1" />
<TextBox Text="Test" Grid.Row="0" Grid.Column="2" />
<TextBox Text="Test" Grid.Row="1" Grid.Column="0" />
<TextBox Text="Test" Grid.Row="1" Grid.Column="1" />
<TextBox Text="Test" Grid.Row="1" Grid.Column="2" />
<TextBox Text="Test" Grid.Row="2" Grid.Column="0" />
<TextBox Text="Test" Grid.Row="2" Grid.Column="1" />
<TextBox Text="Test" Grid.Row="2" Grid.Column="2" />
</Grid>
DependencyProperty
public class GridProperties
{
#region RowCount Property
/// <summary>
/// Adds the specified number of Rows to RowDefinitions. Default Height is Auto
/// </summary>
public static readonly DependencyProperty RowCountProperty =
DependencyProperty.RegisterAttached("RowCount", typeof(int),
typeof(GridProperties),
new PropertyMetadata(-1, RowCountChanged));
// Get
public static int GetRowCount(DependencyObject obj)
{
return (int)obj.GetValue(RowCountProperty);
}
// Set
public static void SetRowCount(DependencyObject obj, int value)
{
obj.SetValue(RowCountProperty, value);
}
// Change Event - Adds the Rows
public static void RowCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (!(obj is Grid) || (int)e.NewValue < 0)
return;
Grid grid = (Grid)obj;
grid.RowDefinitions.Clear();
for (int i = 0; i < (int)e.NewValue; i++)
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
//SetStarRows(grid);
}
#endregion
#region ColumnCount Property
/// <summary>
/// Adds the specified number of Columns to ColumnDefinitions. Default Width is Auto
/// </summary>
public static readonly DependencyProperty ColumnCountProperty =
DependencyProperty.RegisterAttached("ColumnCount", typeof(int),
typeof(GridProperties),
new PropertyMetadata(-1, ColumnCountChanged));
// Get
public static int GetColumnCount(DependencyObject obj)
{
return (int)obj.GetValue(ColumnCountProperty);
}
// Set
public static void SetColumnCount(DependencyObject obj, int value)
{
obj.SetValue(ColumnCountProperty, value);
}
// Change Event - Add the Columns
public static void ColumnCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (!(obj is Grid) || (int)e.NewValue < 0)
return;
Grid grid = (Grid)obj;
grid.ColumnDefinitions.Clear();
for (int i = 0; i < (int)e.NewValue; i++)
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
// SetStarColumns(grid);
}
#endregion
}
Result
