I'm trying to use the StringFormat in XAML to populate the Header text on a TabItem. The code I am using is:
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource TabItemStyle}">
<Setter Property="Header" Value="{Binding MyValue, StringFormat='My Value is {0}'}" />
<EventSetter Event="FrameworkElement.Loaded" Handler="OnTabItemLoaded" />
<EventSetter Event="FrameworkElement.Unloaded" Handler="OnTabItemUnloaded" />
</Style>
</TabControl.ItemContainerStyle>
The problem is my header is only showing the value of MyValue
in the Header and not the formatted text.
Because the Header property is not a string property.
You need to use a headertemplate containing a TextBlock which you can bind the Text property using your stringformat
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource TabItemStyle}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding MyValue, StringFormat='My Value is {0}'}" />
</DataTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="FrameworkElement.Loaded" Handler="OnTabItemLoaded" />
<EventSetter Event="FrameworkElement.Unloaded" Handler="OnTabItemUnloaded" />
</Style>
</TabControl.ItemContainerStyle>