How to set the minimum and maximum width or height of a column or row using WPF

The more I use WPF (Window Presentation Foundation) the more I like it and the more I see the similarity with coding HTML pages or ASP.NET pages. Microsoft has really done a good job at separating the presentation logic from the business logic with WPF. It is much more difficult to do in Windows Forms.

Anyway, while I was building a GUI (Graphical User Interface) where I had implemented a GridSplitter here and here. I realized that I wanted to restrict the minimum size of the column. Without this restriction it is possible for the column to be reduced down to 0 which would result it it no longer being visible. Plus I had buttons and a treeview in column 0 and it didn’t look good when the GridSplitter was partially over them.

It is a relative simple thing to do. To be honest, most technical things are easy to do if you have an example of how to do it. However, finding or creating the example is where much of the complexities enter and is where you need a super duper expert.

Setting the minimum width of a column is achieved like this:

[sourcecode language="xml" padlinenumbers="true" autolinks="false" gutter="false" toolbar="false"]
     <ColumnDefinition Width="Auto" MinWidth="200" />
     <ColumnDefinition Width="Auto" />
     <ColumnDefinition Width="*" />

Setting the minimum height of a row is achieved like this:

[sourcecode language="xml" autolinks="false" gutter="false" toolbar="false"]
     <RowDefinition Height="*" MinHeight="150" />
     <RowDefinition Height="Auto" />
     <RowDefinition Height="2.5*" />

Initially, I was a little skeptical of WPF and wondered why Microsoft had moved in the direction. However, the more I use this technology the more sense it makes. Simply because it does a great job at separating presentation from logic. And if you have ever worked in a maintenance and support team, the fewer places you need to look into for a problem the better and in a majority of cases the bug wouldn’t be in the presentation layer.

Leave a Comment

Your email address will not be published.