Using the UniformGrid panel

UniformGrid creates a mesh of equally sized cells. It's available in WPF and Silverlight and is very handy for creating game-boards for games like chess.

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.

Quirks
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

Dig deeper on Windows Presentation Foundation

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close