WPF GroupBox Header StringFormat Binding

2018-03-01


When you try to use StringFormat binding for a GroupBox Header in WPF, you might find it does not work! Some people mentioned there is a HeaderStringFormat but not StringFormat, but at least in .NET 4.5, we can not find out a HeaderStringFormat.

If we use StringFormat, The following code does not work

 
<GroupBox Header="{Binding path=myText, StringFormat='This is a test string – {0}'}" />

Seems Microsoft only implemented StringFormat binding on limited elements. For example, a TextBlock can use StringFormat Binding and work perfect.

Then we can take advantage of TextBlock. We can use the following code to let StringFormat Binding work for GroupBox header:

<GroupBox> 
  <GroupBox.Header> 
     <TextBlock Text="{Binding path=myText, StringFormat="This is a test string – {0}'}" >
      </TextBlock> 
  </GroupBox.Header> 
</GroupBox>