应用样式键后未选取 TextBlock 的默认样式 [英] Default style for TextBlock not being picked up after applying a style key
问题描述
我有一个
包含这个:
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}"/>
之后 - 只有第二个样式块中的样式被拾起.例如.FontFamily
、FontSize
和 TextWrapping
现在被忽略了.
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屋!