[英]WPF styling tabitem text foreground upon trigger such as IsEnabled, IsMouseOver, etc
我试图使用触发器更改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中为TextBlocks全局设置样式(以保持外观一致)时,前景不会更改,但会保留全局设置的前景颜色。 这是我的TextBlocks样式的方式:
<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文本块按预期方式更改颜色?
非常感谢
新台币
此链接可能会对您有所帮助。 在列表框中更改内容表示者的前景颜色它说明了如何更改内容表示者的前景颜色。
为我工作。 只需更改此:
<Setter Property="Foreground" Value="False"/>
对此:
<Setter Property="Foreground" Value="White"/>
您正在将TabItem的前景色设置为Red,而不是TextBlock。 也许TextBox样式不是从TabItem继承的,因为用户设置的隐式样式优先于触发器设置器。
尝试将绑定添加到TextBlock的父TabItem前景属性。
编辑
像这样
Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}, AncestorLevel=1}, Path=Foreground}"
我的意思是:
<TabItem Header="Summary" x:Name="TabSummary" IsSelected="True" Style="{DynamicResource testTabItemStyle1}">
<Border x:Name="TabSummaryBody" Margin="-5,-5,-5,-5">
<StackPanel Margin="0,30,0,0" HorizontalAlignment="Center">
<TextBlock Text="Please select a document using the tree view on your right to show its properties."
FontSize="16"
Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}, AncestorLevel=1}, Path=Foreground}"/>
</StackPanel>
</Border>
</TabItem>
绑定找到父TabItem并绑定到其Foreground属性。
非常感谢您的帮助,您成功地将我引向正确的方向。
我的意图是更改TabItem的文本(由WPF的ContentPresenter创建),而不是XAML中声明的选项卡中的TextBlock,它可以轻松更改颜色。
问题在于全局样式优先。 由于TextBlock是由WPF创建的,而不是由我声明的,因此我无法访问它。
解决方案是指定ContentPresenter资源,例如:
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" x:Name="Content" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" RecognizesAccessKey="True">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"/>
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</Border>
</Grid>
如您所见,我已经在ContentPresenter资源中设置了TextBlock样式。 因此,很明显,现在ContentPresenter中的任何TextBlocks都应使用父级的Foreground属性,并且由于值强制,这将具有优先权,从而解决了我的问题。
非常感谢大家,
新台币
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.