应用样式键后未选取 TextBlock 的默认样式 [英] Default style for TextBlock not being picked up after applying a style key

查看:27
本文介绍了应用样式键后未选取 TextBlock 的默认样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 包含这个:

I have a <ResourceDictionary> containing this:

<Style TargetType="TextBlock">
    <Setter Property="FontFamily" Value="..\..\Fonts\#Roboto"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="TextWrapping" Value="Wrap"/>
</Style>

这很好用.

现在我添加了另一种样式:

Now I've added another style:

<Style x:Key="MyText" TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
</Style>

但是,在将我的 <TextBlock> 更改为 <TextBlock Style="{StaticResource MyText}"/> 之后 - 只有第二个样式块中的样式被拾起.例如.FontFamilyFontSizeTextWrapping 现在被忽略了.

However, after changing my <TextBlock> to <TextBlock Style="{StaticResource MyText}"/> - only the styles within the 2nd style block are picked up. E.g. the FontFamily, FontSize and TextWrapping are now ignored.

如何为 TextBlock 设置默认值,然后添加到其中?我不想将 x:Key 添加到默认"样式,因为它已经在整个系统中使用了.

How can I have a default for a TextBlock, and then add to it? I don't want to add an x:Key to the 'default' style as this is in use throughout the system already.

推荐答案

我认为您只需要根据类型设置键控样式即可.请参阅下面的示例.

I think you just need to base your keyed style on the type. See example below.

注意 BasedOn="{StaticResource {x:Type TextBlock}}"

<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Foreground" Value="Green" />
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
    <Style x:Key="MyText" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
    </Style>
    <Style x:Key="MyAppendedStyles" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
    </Style>
</Window.Resources>
<StackPanel>
    <TextBlock Text="Hello" />
    <TextBlock Style="{StaticResource MyText}" Text="Style Key" />
    <TextBlock Style="{StaticResource MyAppendedStyles}" Text="Style Key" />
</StackPanel>

这篇关于应用样式键后未选取 TextBlock 的默认样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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