覆盖 WPF 中 contentpresenter 的类型化文本块样式 [英] Override typed textblock style for contentpresenter in WPF

查看:77
本文介绍了覆盖 WPF 中 contentpresenter 的类型化文本块样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了键入的 Textblock 样式(而不是具有键值),以便它适用于所有文本块.

I have defined the Textblock style that are typed (as opposed to have a key value) so that it applies to all the textblocks.

<Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="MyFancyFont"/>
        <Setter Property="FontSize" Value="13.333" />
        <Setter Property="Foreground" Value="Gray" />
</Style>

现在我有一个,比如说,TreeViewItem,当它被选中时,我想显示为蓝色背景和白色前景与深色背景.

Now I have a, say, TreeViewItem, which I'd like to show as blue background and as white foreground against dark background when it's selected.

<!--part of the treeviewitem template-->
<Trigger Property="IsSelected" Value="true">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Background" Value="Black"/>
</Trigger>

为文本块定义本地样式不适用于选择树视图项时的情况,因为项中的文本块仍在选择键入的样式.

Defining a local style for the textblock doesn't work for the situation when the treeview item is selected, as the textblock in the item is still picking up the typed style.

有什么好的方法可以做到这一点,同时仍将文本块样式保持为Typed"?

Is there a good way to do this, while still keeping the textblock style as "Typed"?

推荐答案

this 问题可能对您有所帮助.它展示了如何覆盖隐式样式.

this question may help you. It shows how to override an implicit style.

好的,我理解你的问题,我真的没有直接的解决方案,但无论如何我会告诉你我是如何处理这些事情的:

Ok, I understand your problem and I don't really have a direct solution, but anyways I will tell you how I handle such things:

您确实知道,隐式样式是有作用域的,这意味着:

You do know, that implicit styles are scoped, which means:

    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontFamily" Value="MyFancyFont"/>
            </Style>
        </Grid.Resources>
        <TextBlock>textblock with MyFancyFont</TextBlock>           
    </Grid>
    <TextBlock>textblock with normal font</TextBlock>

我通常会尽量避免在主窗口的资源中为 TextBlock 使用这种隐式样式.相反,我可能会这样做:

I usually try to avoid such an implicit style for TextBlock in the resources of my main window. Instead I might do:

<Application bunch="ofStuff">
    <Application.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStandardStyle">
            <Setter Property="FontFamily" Value="MyFancyFont"/>
        </Style>
    </Application.Resources>
</Application>

然后在那种风格可以是隐含的并且不会造成任何伤害的子区域中,我会写:

then in subareas where that style can be implicit and doesn't cause any harm I will write:

    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBlockStandardStyle}"/>              
        </Grid.Resources>
        <TextBlock>textblock with MyFancyFont</TextBlock>           
    </Grid>

这样我就可以按照我想要的方式来确定范围.也许这种方法可以让您跳过树视图的隐式样式,以便您可以使用触发器!

that way I can scope things how I want. Maybe this approach lets you skip the implicit style for the treeview so you can use your triggers!

这篇关于覆盖 WPF 中 contentpresenter 的类型化文本块样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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