WPF Standard Control Demo App › UniformGrid

UniformGrid Layout

UniformGrid is a simplified panel that automatically arranges its children in equally-sized cells. Unlike the standard Grid, you do not need to define RowDefinitions or ColumnDefinitions โ€” just set the desired number of columns and rows, and UniformGrid does the rest.

Overview

The WPF UniformGrid panel creates a grid where every cell has exactly the same width and height. It distributes the available space evenly among the specified number of columns and rows, making it ideal for button grids, icon palettes, image galleries, and any layout where equal sizing is desired without manual column/row definition.

You can specify Columns and Rows independently. If only Columns is set, the grid grows as many rows as needed to accommodate all children. If both are set, the grid has a fixed cell count; children beyond the cell count overflow without being displayed. If neither is set, UniformGrid arranges children in a square-ish layout, automatically calculating columns based on the child count.

The FirstColumn property introduces an offset โ€” the specified number of cells at the beginning of the first row are left empty. This is useful when you want to start a partial row for alignment purposes, for example when combining UniformGrid items with other elements in a larger layout.

UniformGrid is part of the System.Windows.Controls.Primitives namespace, reflecting its lower-level nature compared to Grid. Because it does not support star sizing, auto sizing, or cell spanning, it is less flexible than Grid but much simpler to configure for uniform layouts.

Screen Preview

uniformgrid demo screen

Demonstrated Properties

The following properties are demonstrated interactively in the WPF Standard Control Demo App. Each property can be configured in real time within the app to observe its behavior.

PropertyValuesDescription
Columns int Use this to fix the number of columns โ€” for example, Columns="7" for a weekly calendar or Columns="4" for a dashboard tile grid. All columns receive an equal share of the available width, and rows are added automatically to accommodate all children. Pitfall: if you specify only Columns and leave Rows at 0, the row count is calculated automatically from the child count. This works well for a growing list of items. However, if both Columns and Rows are set and their product is less than the number of children, the excess children are silently dropped from the layout without any error โ€” keep the child count consistent with the cell count.
Rows int Use this to fix the number of rows in the grid. When set, each row receives an equal share of the available height. Leave at 0 (default) to let the grid grow as many rows as needed based on the child count and the Columns value. Pitfall: when both Rows and Columns are specified, the grid has a fixed cell capacity. Children beyond that capacity are not rendered โ€” there is no automatic expansion or overflow indicator. Monitor the child count carefully when both values are fixed.
FirstColumn int Use this to insert empty cells at the start of the first row, creating an offset. The classic use case is a monthly calendar: if the month starts on a Wednesday, set FirstColumn="3" (0 = Sunday) so that the day numbers align correctly under the weekday headings. Pitfall: if FirstColumn is equal to or greater than the Columns value, the offset is silently ignored. Ensure the day-of-week index (0-based) is always less than the column count.
Background (Panel) Brush Sets the background fill of the panel area including empty cells. A non-null Background also makes the panel hit-testable, allowing mouse events to be caught in empty cells or gaps between items. Pitfall: the default null background disables hit-testing on background areas โ€” set Background="Transparent" if you need the panel to capture mouse events in empty space.
ZIndex (Panel attached) int Controls the draw order of sibling children when they overlap. Higher values render in front. Typically not needed in a UniformGrid because cells do not overlap, but useful if a child uses a negative Margin to deliberately extend beyond its cell boundaries. Pitfall: ZIndex affects only siblings within the same parent panel and has no effect across different containers.

XAML Example

A 4-column UniformGrid used as a color swatch palette:

<UniformGrid Columns="4" Rows="2" Width="200" Height="100">
  <Button Background="Red" />
  <Button Background="Orange" />
  <Button Background="Yellow" />
  <Button Background="Green" />
  <Button Background="Blue" />
  <Button Background="Indigo" />
  <Button Background="Violet" />
  <Button Background="White" />
</UniformGrid>

<!-- Auto-column count -->
<UniformGrid Width="300">
  <Button Content="1" />
  <Button Content="2" />
  <Button Content="3" />
  <Button Content="4" />
  <Button Content="5" />
</UniformGrid>

Common Use Cases

Tips and Best Practices

Related Controls

Source Code

The source code for this demo screen is available on GitHub. Use the built-in code view buttons in the app to see the exact XAML for each property.

View UniformGrid source code on GitHub →