默认 TextBlock 样式覆盖按钮文本颜色 [英] Default TextBlock style overriding button text color

查看:23
本文介绍了默认 TextBlock 样式覆盖按钮文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题出现在 .NET 3.5 SP1 中的 WPF,可以描述如下:

My problem occurs with WPF in .NET 3.5 SP1 and can be described as follows:

我有一个默认的 Style 在我的 UI 中点击所有 TextBlock 元素.这是它的样子:

I have a default Style hitting all TextBlock elements in my UI. That is how it looks:

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
   <Setter Property="Foreground" Value="Red"/>
</Style>

这适用于所有 TextBlock.除此之外,我有一个 Button 样式,包括一个 ControlTemplate,看起来像这样(缩短):

That works fine for all TextBlocks. In addition to that I have a Button style including a ControlTemplate that looks like this (shortened):

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}">
   <Setter Property="Foreground" Value="Green"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="Border" 
                    Background="{TemplateBinding Background}" 
                    BorderThickness="{TemplateBinding BorderThickness}" 
                    Padding="{TemplateBinding Padding}" 
                    Height="24" 
                    BorderBrush="{TemplateBinding BorderBrush}">
               <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                 TextBlock.Foreground="{TemplateBinding Foreground}"/>
            </Border>
            <ControlTemplate.Triggers>...</ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

注意 ContentPresenter 中的 TextBlock.Foreground="{TemplateBinding Foreground}" 行.这应该将按钮文本设置为绿色,实际上在 Visual Studio 的设计器视图中也是如此.但是当我编译并运行程序时,按钮文本是红色的,文本颜色由默认的 TextBlock 样式设置.我通过 Snoop 验证了这一点.

Notice the line TextBlock.Foreground="{TemplateBinding Foreground}" in the ContentPresenter. This should set the button text to green and in fact it does in the designer view of Visual Studio. But when I compile and run the program the button text is red, the text color is set by the default TextBlock style. I verified this with Snoop.

如何防止默认TextBlock 样式覆盖TextBlock.Foreground 值?ContentPresenterOverridesDefaultStyle 属性在这种情况下没有帮助.

How can I prevent the defaultTextBlock style from overriding the TextBlock.Foreground value? The OverridesDefaultStyle property of ContentPresenter doesn't help in this case.

有什么想法吗?

推荐答案

请参阅 此链接

发生这种情况是因为ContentPresenter 创建一个 TextBlock对于字符串内容,从那以后TextBlock 不在可视化树中,它将查找到应用程序级别资源.如果你定义了一种风格用于应用程序中的 TextBlock级别,然后将应用于ContentControl 中的这些 TextBlock

This happends because the ContentPresenter creates a TextBlock for a string content, and since that TextBlock isn't in the visual tree, it will lookup to Application level resource. And if you define a style for the TextBlock at Application level, then it will be applied to these TextBlock within ContentControl

一种解决方法是定义一个System.String 的数据模板,其中我们可以明确地使用默认值TextBlock 显示内容.你可以将该 DataTemplate 放在你定义的同一个字典TextBlock 样式使这个DataTemplate 将应用于无论受什么 ContentPresenter 影响你的风格.

A workaround is to define a DataTemplate for System.String, where we can explicitly use a default TextBlock to display the content. You can place that DataTemplate in the same dictionary you define the TextBlock style so that this DataTemplate will be applied to whatever ContentPresenter effected by your style.

尝试将其添加到 ResourceDictionary

Try adding this to the ResourceDictionary

<DataTemplate DataType="{x:Type sys:String}">
    <TextBlock Text="{Binding}">
        <TextBlock.Resources> 
            <Style TargetType="{x:Type TextBlock}"/>
        </TextBlock.Resources>
    </TextBlock>
</DataTemplate>

这篇关于默认 TextBlock 样式覆盖按钮文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆