public class CustomUniformGrid : UniformGrid
{
protected override Size MeasureOverride(Size availableSize)
{
// Calculate the number of rows and columns
int columns = Columns;
int rows = Rows;
if (columns == 0)
{
columns = (int)Math.Ceiling(Math.Sqrt(InternalChildren.Count));
}
else
{
rows = (int)Math.Ceiling((double)InternalChildren.Count / columns);
}
// Calculate the width and height for each cell
double cellWidth = availableSize.Width / columns;
double cellHeight = 0;
// Measure each child to find the maximum height in each row
int columnIndex = 0;
foreach (UIElement child in InternalChildren)
{
child.Measure(new Size(cellWidth, double.PositiveInfinity));
cellHeight = Math.Max(cellHeight, child.DesiredSize.Height);
columnIndex++;
if (columnIndex >= columns)
{
columnIndex = 0;
// Move to the next row
cellHeight = 0;
}
}
// Measure each child again with the calculated cell height
foreach (UIElement child in InternalChildren)
{
child.Measure(new Size(cellWidth, cellHeight));
}
// Return the total size needed for the UniformGrid
double totalWidth = Math.Min(availableSize.Width, columns * cellWidth);
double totalHeight = Math.Min(availableSize.Height, rows * cellHeight);
return new Size(totalWidth, totalHeight);
}
}
I am expecting Wrappanel behaiviour in Uniform Grid and i dont want to use another panel in it
New contributor
SWETA BHARTI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.