In WPF there are Panels that get used often: the Canvas, DockPanel, Grid, StackPanel, and even the WrapPanel. These Panels feel a lot of love. The UniformGrid, not so much. While most Panels are displayed proudly in System.Windows.Controls, the UniformGrid has been pushed into the Primitives namespace along side the TabPanel and the ToolBarPanel, both of which are too specialized to be given any real attention.
The UniformGrid is a great Panel to keep in mind. Once you do you’ll use it more often than you think. It does what the name implies, it lays out its children in a grid with uniform cells. You can specify neither the Rows or Columns, in which case it creates a square grid (3 x 3, 4 x 4, etc). You can specify either Rows or Columns, in which case it forces that number of cells in the given direction. For example, specifying 2 columns and providing six children would result in a 2w x 3h grid, providing one child would result in a 2w x 1h grid. Finally, you can specify both Rows and Columns, creating a grid of a fixed size. If you specify both Rows and Columns and provide more children than there are cells then the extra children will not be displayed. Finally, by placing the UniformGrid in a ScrollViewer and binding the size of the UniformGrid to a multiple of the actual size of it’s parent, you can achieve some great results.
Here are some of my common uses for the UniformGrid:
- Laying out icons in a uniform way (think of the iPhone home screen)
- Photo or video galleries where every item should be the same size
- Specifying Rows=”1″ and providing only two children for side by side comparisons
- Even spacing of children with a fixed size
- Using it as a uniform StackPanel by specifying either Rows=”1″ or Columns=”1″
- A quick way to layout controls, such as in a sandbox application
If you have any more good uses for this neglected little fella leave a comment.