Using the UniformGrid panel

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.

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

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

in the ListBox and that the UniformGrid is arranging the items as expected.



This was first published in June 2010

Disclaimer: 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.