3

这似乎很简单,但我很难找到答案。

我有一个 WPF 窗口,需要为不同的区域使用标题和子标题。当然,我可以为每个元素包含 stlying 内联,但我真的很想保持样式分开。在语义上区分各种标题/子标题/“正常”标签类以便它们可以在单独的 XAML 文档中正确设置样式的正确方法是什么?

谢谢!

4

1 回答 1

10

您可以为资源字典中所需的每个“标签类”设置样式,如下所示:

<Style x:Key="heading" TargetType="Label">
    <Setter Property="FontSize" Value="24" />
</Style>

<Style x:Key="subHeading" TargetType="Label">
    <Setter Property="FontSize" Value="16" />
</Style>

<Style x:Key="normal" TargetType="Label">
    <Setter Property="FontSize" Value="12" />
</Style>

然后在您看来,您只需调用资源并按如下方式使用它:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="Resources/MyResources.xaml">
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <Label Style="{StaticResource heading}" Content="This is a heading!" />
</Grid>
于 2012-08-20T17:31:45.490 回答