我正在尝试使用 XAML 中的 StringFormat 来填充 TabItem 上的标题文本。我正在使用的代码是:
<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>
问题是我的标题只显示
MyValue
的值在标题中而不是格式化的文本中。
最佳答案
因为 Header 属性不是字符串属性。
您需要使用包含 TextBlock 的 headertemplate,您可以使用您的 stringformat 绑定(bind) Text 属性
<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>
关于wpf - 字符串格式的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7409934/