Using the UniformGrid panel
One of the least used layout panels in WPF is the UniformGrid. Perhaps it is because it is so simple it gets overlooked when choosing a layout container. Essentially, a UniformGrid creates a mesh of equally sized cells. You have no direct control over the size of the cells, you specify the number of rows and columns and the panel does the rest. You can think of it as a simplified Grid.
It's available in WPF and Silverlight and is very handy for creating game-boards for games like chess, concentration and minesweeper. Here's a sample 3x3 layout.
As you can see, the location of each child of the UniformGrid is in the order they are added in the XAML.
What happens if you put more children in the UnformGrid then you have cells defined? Interestingly, the panel will still load the children. In the next example there are nine cells and ten children. The margin of the UniformGrid has been modified so that we can see more of the its content.
The following code determines what element is the parent for the 10th button. It proves that the button is a child of the UniformGrid.
Using the UniformGrid in an ItemsPanelTemplate
Naturally you can use a UniformGrid in an ItemsPanelTemplate. This makes it simple to create uniform sized grids of data.
In the following screen shot you can see the penguin image is selected in the ListBox and that the UniformGrid is arranging the items as expected.
This was first published in June 2010
Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.