如何阻止 ToolTips 继承我的 TextBlock 样式? [英] How can I stop ToolTips from inheriting my TextBlock style?

查看:36
本文介绍了如何阻止 ToolTips 继承我的 TextBlock 样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序资源中定义了一种样式,我希望我的所有文本块都具有惰性:

I've defined a style within my application resources that I want all of my textblocks to inerit :

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="1" ShadowDepth="0" />
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontFamily" Value="Arial" />
    <Setter Property="FontWeight" Value="Bold" />
</Style>

我不想经历并明确指定每个文本块上的样式 - 我只是希望它们都能自然地继承这种样式.

I don't want to have to go through and explicitly dictate the style on each textblock - I just want all of them to inherit this style naturally.

不幸的是,当我定义工具提示时,该工具提示也会采用这种样式.我猜这是因为这个东西在它的设计中加入了一个 TextBlock.

Unfortunately, when I define a tooltip, that tooltip also picks up this style. I'm guessing it's because the thing incorporates a TextBlock somewhere in it's design.

我可以接受的是必须通过并设置每个定义的工具提示的样式(因为它们在我的应用程序中使用较少),因此如果有某种方法可以定义将覆盖继承的文本块样式的工具提示样式,我'我没问题.

What I AM okay with is having to go through and style each defined tooltip ( since they are used less ubiquitously throughout my application ), so if there's some way to define a tooltip style that will override the inherited textblock style, I'm fine with that.

那么,我该怎么做才能阻止我的工具提示继承 TextBlock 样式?

So, what can I do to stop my tooltips from inheriting the TextBlock style?

推荐答案

TextBlock 上设置全局隐式样式是一个糟糕的主意,这就是原因.TextBlock 是显示文本的原语.最好只在需要的地方设置隐式 TextBlock 样式,而不是普遍设置.

It's a terrible idea to set a global implicit style on TextBlock, and this is why. TextBlock is the primitive that displays text. It's much better to set the implicit TextBlock style only where it's needed, not universally.

或者考虑使用 Label 而不是 TextBlock 作为样式文本实例,并具有隐式 Label 样式.这就是 Label 存在的原因之一.您可以设置填充/边距等样式,使其看起来完全符合您的要求.

Or consider using Label instead of TextBlock for the styled text instances, and have an implicit Label style. That's one reason why Label exists. You can style the padding/margin etc. to make it look exactly the way you want.

但是,如果您想要快速、肮脏和简单的方法来解决错误的决定,您可以使用隐式风格本地化的风格内技巧,爷爷曾经在阿拉曼镇压隆美尔将军:

But if you want the quick and dirty and easy way to work around a bad decision, you can use the implicit-style-localized-within-a-style trick that grandpa used to flummox General Rommel at El Alamein:

<Style TargetType="ToolTip">
    <Style.Resources>
        <!-- Implicit style for TextBlocks within ToolTips -->
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontWeight" Value="Regular" />
            <Setter Property="Effect" Value="{x:Null}" />
        </Style>
    </Style.Resources>

    <Setter Property="Foreground" Value="Black" />
    <Setter Property="FontWeight" Value="Regular" />
    <Setter Property="Effect" Value="{x:Null}" />
</Style>

这篇关于如何阻止 ToolTips 继承我的 TextBlock 样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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