DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Create a WPF Application

Trending

  • How to Migrate Vector Data from PostgreSQL to MyScale
  • An Introduction to Build Servers and Continuous Integration
  • Memory Management in Java: An Introduction
  • Unveiling Vulnerabilities via Generative AI

Think twice before putting another grid inside a WPF window

Denzel D. user avatar by
Denzel D.
·
Aug. 25, 10 · Interview
Like (0)
Save
Tweet
Share
40.40K Views

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.

Windows Presentation Foundation

Opinions expressed by DZone contributors are their own.

Related

  • Create a WPF Application

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: