带图像的 WPF TabItem 样式
我有一个带有 TabItem
的 TabControl
,TabItem
有文本和图标。为此,我必须向 TabItem
添加一个 StackPanel
。
但是,一旦添加了 StackPanel
,我就无法再控制文本的默认样式。
资源:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Name="tabItem">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="1" Name="tabItemStyle" Background="Transparent" BorderBrush="Transparent" BorderThickness="1,1,1,0" CornerRadius="3,3,0,0" SnapsToDevicePixels="True" Margin="0, 0, 5, 0">
<ContentPresenter x:Name="ContentSite" TextBlock.Foreground="White" TextBlock.FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="14,3,18,3">
</ContentPresenter>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tabItemStyle" Property="Background" Value="#ecf3f9" />
<Setter TargetName="tabItemStyle" Property="BorderBrush" Value="#29458e" />
<Setter TargetName="tabItemStyle" Property="BorderThickness" Value="1,1,1,0" />
<Setter TargetName="ContentSite" Property="TextBlock.Foreground" Value="#29458e" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="tabItemStyle" Property="Background" Value="#6381be" />
<Setter TargetName="tabItemStyle" Property="BorderBrush" Value="#97acd4" />
<Setter TargetName="tabItemStyle" Property="BorderThickness" Value="1,1,1,0" />
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TabItem:
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="/images/icons/_24/reports.png" Width="24" />
<TextBlock VerticalAlignment="Center" Margin="5, 0" >Reports</TextBlock>
</StackPanel>
</TabItem.Header>
</TabItem>
相关行是资源(样式)中的ContentPresenter
。 TextBlock.Foreground="White"
不再起作用。我可以理解为什么,但不知道还能如何做到这一点。有什么想法吗?
I have a TabControl
with TabItem
s, The TabItem
s have text and an icon. To do this I have to add a StackPanel
to the TabItem
.
However, once I add the StackPanel
, I can no longer control the default style of the text.
Resource:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Name="tabItem">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="1" Name="tabItemStyle" Background="Transparent" BorderBrush="Transparent" BorderThickness="1,1,1,0" CornerRadius="3,3,0,0" SnapsToDevicePixels="True" Margin="0, 0, 5, 0">
<ContentPresenter x:Name="ContentSite" TextBlock.Foreground="White" TextBlock.FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="14,3,18,3">
</ContentPresenter>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="tabItemStyle" Property="Background" Value="#ecf3f9" />
<Setter TargetName="tabItemStyle" Property="BorderBrush" Value="#29458e" />
<Setter TargetName="tabItemStyle" Property="BorderThickness" Value="1,1,1,0" />
<Setter TargetName="ContentSite" Property="TextBlock.Foreground" Value="#29458e" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="tabItemStyle" Property="Background" Value="#6381be" />
<Setter TargetName="tabItemStyle" Property="BorderBrush" Value="#97acd4" />
<Setter TargetName="tabItemStyle" Property="BorderThickness" Value="1,1,1,0" />
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TabItem:
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="/images/icons/_24/reports.png" Width="24" />
<TextBlock VerticalAlignment="Center" Margin="5, 0" >Reports</TextBlock>
</StackPanel>
</TabItem.Header>
</TabItem>
The relevant line is the ContentPresenter
in the Resource (Style). The TextBlock.Foreground="White"
no longer works. I can see why but cannot find out how else to do this. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在实际的
TabItem
上设置相关属性即可。例如,请注意,如果您将以下内容放在样式的顶部:文本仍然会发生变化。您不需要引用
ContentPresenter
的TextBlock
,即使引用了,您也可以应用TemplateBindings
到其属性,以便当TabItem
的属性更改时,ContentPresenter
也是如此。Simply set the relevant properties on the actual
TabItem
. For instance, notice if you put the following at the top of the style:the text still changes. You don't need to refer to the
ContentPresenter
'sTextBlock
, and even if you did, you could applyTemplateBindings
to its properties so that whenTabItem
's properties changed, so didContentPresenter
's.