对TextBlock和Run元素使用相同的样式

Bor*_* B. 5 .net wpf styles

我有一个WPF样式设置TextDecorations依赖属性TargetType: TexBlock.我需要对某些Text元素中的某些Run元素使用相同的样式,而TextBlock本身并不使用上述样式.如何在不重复相同风格的情况下实现这一目标TargetType呢?

H.B*_*.B. 5

只是不要指定TargetType属性但限定属性,例如:

<Style x:Key="CommonStyle">
    <Setter Property="Inline.TextDecorations" Value="StrikeThrough" />
</Style>
Run Code Online (Sandbox Code Playgroud)
<TextBlock Style="{StaticResource CommonStyle}" Text="Lorem Ipsum" />
<TextBlock>
    <Run Style="{StaticResource CommonStyle}" Text="Lorem" />
    <Run Text="Ipsum" />
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

如果您想进一步开发可以使用的样式BasedOn,这也允许通过不在派生样式上设置键来隐式应用所述样式.