Think twice before putting another grid inside a WPF window
Join the DZone community and get the full member experience.Join For Free
Before I go any further with this discussion, I must say that I have nothing against grids or any container control that can be used in a WPF application. I’m talking about some possible incorrect usage of the grid caused by outdated habits, as well as showing a good solution to implement the same functionality.
So here is the plan - I am working on an application that has several separate zones. There is a sidebar, a custom status bar and a zone designed for a custom toolbar. The general layout for it would look like this:
So what’s the first idea that comes to mind when you need to implement this? If you would work on a WinForms application, you’d probably throw a bunch of Panel controls on the form and dock them properly. Some developers who make the transition from Windows Forms to WPF often try to do the same in a WPF application.
What they don’t know (yet) is that this approach is generally the wrong way to take. Not that it will have terrible consequences, but it will ensure the use of some extra controls that could be avoided.
So what’s up with adding a few more grids on the window? First of all, if I wanted to re-create the UI structure like it is shown on the image above, I will have to have at least three grids (highlighted – you can consider any container control out there). And you might actually consider adding a fourth container for the main content. Four additional controls that need to be correctly docked and anchored inside the form - not really the best option when you plan to do work on a lot of UI modifications, since this will possibly create a mess – when you will incorrectly anchor something, it might eventually cause overlaps and misplacements.
The alternative scenario to this would be using a single grid and dividing it in several rows and columns (I am yet to see a massive adoption of this method to use the Grid control, though). For the UI shown, I created a sample window:
<Window x:Class="WPF_SB.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> </Grid></Window>
Now, inside the Grid markup element, I can define specific rows that will be present inside the main grid. This is done through Grid.RowDefinitions:
<Grid.RowDefinitions> <RowDefinition Height="70"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="20"></RowDefinition></Grid.RowDefinitions>
Now I have three separate rows that make the grid look like this:
For the first and last row, I am explicitly defining the height, while for the middle one I am just specifying that it will take the rest of the space available.
I would also need a column for the sidebar, so all I have to do is set the Grid.ColumnDefinitions:
<Grid.ColumnDefinitions> <ColumnDefinition Width="150"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition></Grid.ColumnDefinitions>
That’s it! You’ve got the basic structure for the UI set up. Now you can reference controls to their own cell by using Grid.Row and Grid.Column properties. For example, if I want to place a TextBox control inside the main (the largest) cell, I can do it like this (given that the control is placed inside the defined grid):
<TextBox Grid.Row="1" Grid.Column="1"></TextBox>
But what about the ribbon toolbar placeholder? There are obviously two cells in the top row and there is no possibility to set the Grid.Column property twice, but I’d like to have it span across both cells. In this case, Grid.ColumnSpan comes to save the day and it gives you the ability to span a control across multiple columns.
Here is an example where I am spanning a button over the top two columns:
<Button Grid.ColumnSpan="2" Content="Test"></Button>
Since the initial point is the topmost left corner, I don’t have to specify the container column and row. The ColumnSpan property defines the number of columns across which the control will be spanned.
The same can be done with rows, and the same button can be spanned across the entire sidebar – all three rows, with the help of RowSpan:
<Button Grid.RowSpan="3" Content="Test"></Button>
What I really like about this method, is that after all I don't have to handle the docking and anchoring (if needed) on my own for the base layout. This way, I can easily modify the UI without being worried about possible overlaps or misalignments, since everything is designated to a specific column and/or row.
Opinions expressed by DZone contributors are their own.