I was trying out to create a Custom Grid where it re sizes and relocates it's children dynamically, based on the largest available child dimension and window dimension. This means I will have to redraw my rows and columns definitions on the fly. The below given is my Custom Grid Code
public class GridPanel : Grid
{
#region Fields
Size maxItemSize;
int maxPossibleColumns = 0;
int maxPossibleRows = 0;
#endregion
#region Overrides
protected override Size MeasureOverride(Size constraint)
{
this.ShowGridLines = true;
GetMaxItemSize(constraint);
//SetupGridCells(constraint);
return constraint;
}
protected override Size ArrangeOverride(Size arrangeSize)
{
//SetupGridCells(arrangeSize);
return base.ArrangeOverride(arrangeSize); //Caught a nullReferrence exception
}
#endregion
#region Methods
private void GetMaxItemSize(Size availableSize)
{
foreach (UIElement item in Children)
{
item.Measure(availableSize);
maxItemSize = item.DesiredSize.Width > maxItemSize.Width ? item.DesiredSize : maxItemSize;
}
}
private void SetupGridCells(Size finalSize)
{
ColumnDefinition spaceWidth;
ColumnDefinition cellWidth;
RowDefinition spaceHeight;
RowDefinition cellHeight;
maxPossibleColumns = (int)(finalSize.Width / maxItemSize.Width);
for (int i = 0; i < maxPossibleColumns; i++)
{
spaceWidth = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
cellWidth = new ColumnDefinition { Width = new GridLength(maxItemSize.Width) };
this.ColumnDefinitions.Add(spaceWidth);//***
this.ColumnDefinitions.Add(cellWidth);//***
}
spaceWidth = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
this.ColumnDefinitions.Add(spaceWidth);//***
/////////////////////////////////////////////////////////////////////////////////////////////
maxPossibleRows = (int)(finalSize.Height / maxItemSize.Height);
for (int i = 0; i < maxPossibleRows; i++)
{
spaceHeight = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
cellHeight = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
this.RowDefinitions.Add(spaceHeight);//***
this.RowDefinitions.Add(cellHeight);//***
}
spaceHeight = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
this.RowDefinitions.Add(spaceHeight);
}
#endregion
}
and this is how I'd use the panel
<Panels:GridPanel>
<Rectangle Height="10" Width="10" Fill="Red" />
<Rectangle Height="20" Width="20" Fill="Black" />
<Rectangle Height="30" Width="30" Fill="Green" />
<Rectangle Height="40" Width="40" Fill="Blue" />
<Rectangle Height="50" Width="50" Fill="Aqua" />
<Rectangle Height="60" Width="60" Fill="Wheat" />
<Rectangle Height="70" Width="70" Fill="Blue" />
<Rectangle Height="80" Width="80" Fill="DarkBlue" />
<Rectangle Height="90" Width="90" Fill="DarkGoldenrod" />
<Rectangle Height="100" Width="100" Fill="Gainsboro" />
<Rectangle Height="110" Width="110" Fill="Pink" />
<Rectangle Height="120" Width="120" Fill="Brown" />
</Panels:GridPanel>
SetupGridCells() is the method inside which I'm trying to redraw Rows and columns. The issue is that even if I call this method inside ArrangeOveride() or MeasureOverride(), it throws a nullReference exception in return base.ArrangeOverride(arrangeSize); On further debugging I noticed that this.ColumnDefinitions.Add(spaceWidth);//* (those lines marked with the comment //*) is causing the issue.
Could some one point out what the issue would be? Thanks in advance