WPF 样式 tabitem 文本前台触发器,例如 IsEnabled、IsMouseOver 等
我正在尝试使用触发器更改 WPF 选项卡项的标题文本块的前景文本颜色。 这对于大多数(更简单的)场景来说效果很好,但当 TextBlocks 具有全局样式时就不行了。
因此,这个简单的“鼠标悬停”触发器将在更改前景色方面发挥作用:
<Style x:Key="testTabItemStyle1" TargetType="{x:Type TabItem}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Bd" Background="White" BorderBrush="Gray" BorderThickness="1,1,1,0">
<ContentPresenter HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" x:Name="Content" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" ContentSource="Header"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Bd" Value="Black"/>
<Setter Property="Foreground" Value="False"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
问题是,当在 App.xaml 中对 TextBlock 进行全局样式设置(为了保持一致的外观)时,前景不会更改,但会保留全局样式前景色。 这就是我的 TextBlock 的样式:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Brown"/>
<Setter Property="Margin" Value="4,0,4,0"/>
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
所以我的问题是显式定义的样式分配(在 TabItem 的触发器中)不应该优先吗? 更重要的是,如何解决这个问题,而不单独为所有文本块分配样式,但让 TabItem 文本块按预期更改颜色?
非常感谢
NT
I'm trying to change the foreground text colour of a WPF tab item's header textblock using triggers. This works fine for most (simpler) scenarios but not when TextBlocks have been globally styled.
So this simple "mouse over" trigger would work in terms of changing the foreground color:
<Style x:Key="testTabItemStyle1" TargetType="{x:Type TabItem}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Bd" Background="White" BorderBrush="Gray" BorderThickness="1,1,1,0">
<ContentPresenter HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" x:Name="Content" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" ContentSource="Header"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Bd" Value="Black"/>
<Setter Property="Foreground" Value="False"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is that when TextBlocks are globally styled in App.xaml (for maintaining a consistent look), the foreground does not change, but retains the globally styled foreground color. This is how my TextBlocks are styled:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Brown"/>
<Setter Property="Margin" Value="4,0,4,0"/>
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
So my question is shouldn't the explicitly defined style assignment (in TabItem's trigger) have precedence? More importantly, how do I work around this without assigning styles to all my textblocks individually but having the TabItem textblock change color as expected?
Many thanks
NT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我有用。 只需将其更改
为:
Works for me. Just had to change this:
to this:
您将 TabItem 的前景色设置为红色,而不是 TextBlock。
也许 TextBox 样式不是从 TabItem 继承的,因为用户设置的隐式样式优先于触发器设置器。
尝试将绑定添加到 TextBlock 的父 TabItem Foreground 属性。
编辑
像这样
You are setting the foreground color of a TabItem to Red, not the TextBlock.
Maybe the TextBox style is not inherited from TabItem because user set implicit styles have precedence over trigger setters.
Try adding a binding to the TextBlock's parent TabItem Foreground property.
EDIT
Like this
我的意思是这样的:
Binding 找到父 TabItem 并绑定到它的 Foreground 属性。
What I meant was this:
Binding finds the parent TabItem and binds to its Foreground property.
非常感谢您的帮助,您成功地将我引向正确的方向。
我的目的是更改 TabItem 的文本(由 WPF 的 ContentPresenter 创建),而不是选项卡中的 TextBlock,后者在 XAML 中声明并且可以轻松更改颜色。
问题在于全局风格优先。 由于 TextBlock 是由 WPF 创建的而不是由我声明的,因此我无法访问它。
解决方案是指定 ContentPresenter 资源,如下所示:
如您所见,我已在 ContentPresenter 资源中设置了 TextBlock 样式。
显然,现在 ContentPresenter 中的任何 TextBlock 都应使用父级的 Foreground 属性,并且由于值强制,这将优先,解决我的问题。
非常感谢大家,
NT
Many thanks for your help, you successfully steered me to the right direction.
My intention was to alter the TabItem's text (Created by WPF's ContentPresenter) as opposed to the TextBlock within the tab which is declared in XAML and can easily change colour.
The problem was with the global style taking precedence. And as the TextBlock is created by WPF rather than declared by me, I could not access it.
The solution was to specify the ContentPresenter Resources, as such:
As you can see I have set the TextBlock style within the ContentPresenter resources.
So obviously now any TextBlocks within the ContentPresenter shall use the parent's Foreground property and this will take precedence due to value coercion, solving my problem.
Many thanks to all,
NT