通常,您可以创建针对公共基类的样式,然后创建从基样式派生的空样式以针对特定类。但是,在 TextBlock 和 Run 的情况下,它们不共享一个公共基类,事实上,由于 Run 不是从 FrameworkElement 派生的,它甚至没有 Style 属性。
但是,如果您询问 Run 是否会继承其父 TextBlock 的前景/字体属性,那么它会。但是您不能独立于包含 TextBlock 的 Run 应用此样式。
另一种选择是为前景画笔和字体粗细创建静态资源,如下所示:
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.Resources>
<FontWeight x:Key="SubtitleFontWeight">Light</FontWeight>
<SolidColorBrush x:Key="SubtitleForeground" Color="#787878" />
</Grid.Resources>
<TextBlock>
<Run Text="Hello " />
<Run Text="World!"
Foreground="{StaticResource SubtitleForeground}"
FontWeight="{StaticResource SubtitleFontWeight}" />
</TextBlock>
</Grid>