HeaderFormatString and ContentFormatString

I started exploring both HeaderFormatString and ContentFormatString and ran into a little weirdness.

First thing to note is this:

If you set the HeaderTemplate or HeaderTemplateSelector property of a HeaderedContentControl, the HeaderStringFormat property is ignored. MSDN

There are quite a few gotchas like this in WPF to watch out for.

Second thing to note is that these properties aren’t the same as doing this:
headerstringformatxaml3
HeaderedContentControl and HeaderStringFormat are used specifically for classes that implement IFormattable. HederStringFormat formats the header, and ContentStringFormat formats the content. The value of either property is the format that gets passed to your classes implementation if IFormattable.ToString. You can read the full example on MSDN. But here is the gist of how to make it work.

headerstringformatxaml2
headerstringformatxaml

This TabItem will now display “This is my formatted string” in the header, and the content will be “this is my non-formatted string”.

image.png

There a couple things to keep in mind. Typically these properties would be used in the context of an ItemsControl. The HeaderStringFormat would not be bound in this way, and instead will have the default binding provided by the ItemContainer of the HeaderedItemsControl. For instance if you set the ItemsSource property of the TabItem, then it will automatically wire up the header and the content binding for you, and all you have to do is supply the formatting value you want.

Last, but not least, I was able to get everything working properly with a GroupBox and TabItem, but not so much luck with an expander and I’m not sure why. The expander handles the ContentStringFormat properly, but not the HeaderContentStringFormat. This is suprising considering that the both inherit from HeaderContentControl…

kick it on DotNetKicks.com

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: