我有一个 WPF 样式,为 TargetType: TexBlock
设置 TextDecorations
依赖属性。我需要对某些 TextBlock 中的某些 Run 元素使用相同的样式,而 TextBlock 本身并不使用上述样式。如何在不使用不同的 TargetType
重复相同样式的情况下实现此目的?
最佳答案
只需不指定 TargetType
但限定属性,例如:
<Style x:Key="CommonStyle">
<Setter Property="Inline.TextDecorations" Value="StrikeThrough" />
</Style>
<TextBlock Style="{StaticResource CommonStyle}" Text="Lorem Ipsum" />
<TextBlock>
<Run Style="{StaticResource CommonStyle}" Text="Lorem" />
<Run Text="Ipsum" />
</TextBlock>
如果你想进一步发展这种风格,你可以使用BasedOn
,这还允许通过不在派生样式上设置键来隐式应用所述样式。
关于.net - 对 TextBlock 和 Run 元素使用相同的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7835951/