March 12, 2012

Ditch the Grids and Use DockPanels

I have worked extensively with WinForms and WPF/Silverlight and noticed that docking/dock panels are not used that often.  At least this is the case where I work.  I find this surprising given their power and ease of use.

Using XAML, why would a programmer choose a complex Grid/StackPanel setup over a DockPanel?  Or on the WinForms side, set the anchor properties for control rather than simplify things with the dock property?  I see this stuff all the time.  Perhaps it's a lack of knowledge.  Maybe there is a performance penalty that I don't know about.

There are a couple of quirks with docking that confused me at first.  In WinForms, when setting a control's Dock property to Fill, it may appear behind the other controls on the form rather than filling the remaining space.  The fix is to right-click the control in the designer and choose "Bring to Front".  It will resize to fill the remaining space as one would expect.

In WPF/Silverlight, there is no Dock property.  Instead the DockPanel control performs the same function.  Child controls of the DockPanel set the attached Dock property to dock themselves.  The values may be Left, Right, Top, and Bottom, but there is no Fill value.  Instead the last child control in the XAML is assumed to be the fill control even if it sets a value explicitly.  Or the fill panel can be turned off by adding LastChildFill = "False" on the DockPanel.  This can be a little strange especially coming from WinForms.

These are the only two oddities I've come across; otherwise I have not seen any obvious performance or functionality issues.  In my experience, docking works beautifully and is my preferred method for layout.

© Joe Buschmann 2020