Charles Petzold



The Secret Identity of StatusBar

May 3, 2006
New York City

Exploring templates in the WPF can sometimes turn up some interesting information.

In the WPF, ItemsControl is the control that implements the storage of multiple items; from ItemsControl descend such crucial controls as Menu, MenuItem, TreeView, TreeViewItem, ToolBar, ListBox, ListView, and StatusBar.

In keeping with the "lookless control" philosophy of WPF, ItemControls defines a property named ItemsPanel of type ItemsPanelTemplate. This is basically the Panel element that the particular ItemsControl derivative uses to display multiple items.

Most of the classes that derive from ItemsControl use a StackPanel by default to display items. Menu, however, uses a WrapPanel. That's why the top-level menu wraps. ListBox and ListView use a VirtualizingStackPanel, which is optimized for large amounts of data.

The type of panel that StatusBar uses to display its children is unique among all the other derivatives of ItemsControl. It's the DockPanel. I was flabbergasted when I discovered this (using a program that will appear in Chapter 25 of my book) because I had struggled with positioning multiple items on a StatusBar and couldn't quite get it to work. Everything seemed to want to go over to the left except for the last item, which could have its HorizontalAlignment property set to Right. Well, duh! By default, DockPanel assumes left docking except for the last item, which fills the remainder of the area.

In short, to use StatusBar intelligently, you should set the DockPanel.Dock attached property on the status bar items. Naturally, I haven't found any documentation that comes anywhere close to suggesting this.

The StatusBarDemo.xaml file demonstrates the use of DockPanel.Dock on StatusBarItem. (As is normal, the StatusBar itself is docked to the bottom of the page.) I've also used Separator to separate the status bar items.

Of course, because this DockPanel is defined in a template, it's very easy to replace it. The StatusBarWithUniformGrid.xaml file substitutes a 3-column UniformGrid for the DockPanel and lets you structure a StatusBar with multiple rows and columns.