WPF:如何覆盖XAML中全局定义的文本块的标题单元格的前景属性
全局定义的灰色文本块WPF:如何覆盖XAML中全局定义的文本块的标题单元格的前景属性,wpf,xaml,Wpf,Xaml,全局定义的灰色文本块 <Style TargetType="{x:Type TextBlock}"> <Setter Property="Foreground" Value="{StaticResource BR_SE_Gray}" /> <Setter Property="TextTrimming" Value="CharacterEllipsis"/> <Setter Property="FontSize" Value="11
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{StaticResource BR_SE_Gray}" />
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="FontFamily" Value="Arial Unicode MS"/>
<Style.Triggers>
<Trigger Property="controls:TextBlockService.IsTextTrimmed" Value="True">
<Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
</Trigger>
</Style.Triggers>
我想在下面提到的代码中的网格视图标题单元格中使用黑色。在上面的代码块中,我想要黑色的前景属性。全局定义的文本块颜色为灰色。如何在下面的代码中覆盖它并使其变为黑色
通过向上遍历逻辑树来解析资源。因此,您可以通过在ContentControl的resource部分下为textBlock指定resource来覆盖resource,并在那里将前台设置为黑色 这样,
新样式将应用于ContentControl下的所有文本块
,而ContentControl之外的文本框将继续使用您的全局样式
确保为样式设置了BasedOn
,以便样式继承在基本样式中设置的其他属性-
<ContentControl>
<ContentControl.Resources>
<Style TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Foreground" Value="Black" />
</Style>
</ContentControl.Resources>
</ContentControl>
通过向上遍历逻辑树来解析资源。因此,您可以通过在ContentControl的resource部分下为textBlock指定resource来覆盖resource,并在那里将前台设置为黑色
这样,新样式将应用于ContentControl下的所有文本块
,而ContentControl之外的文本框将继续使用您的全局样式
确保为样式设置了BasedOn
,以便样式继承在基本样式中设置的其他属性-
<ContentControl>
<ContentControl.Resources>
<Style TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Foreground" Value="Black" />
</Style>
</ContentControl.Resources>
</ContentControl>
\在contentcontrol中,而不是将前台绑定到templateparent,设置所需的值,即,而不是前台=“{TemplateBinding foreground}”直接执行前台=“Black”\在contentcontrol中,而不是将前台绑定到templateparent,设置所需的值,即,而不是前台=“{TemplateBinding foreground}”直接执行前台=“黑色”