如何在WPF/Silverlight/XAML中标题标题?

Hol*_*osa 3 silverlight wpf xaml

这看起来很简单,但我很难找到答案.

我有一个WPF窗口,需要使用不同区域的标题和子标题.当然,我可以在每个元素中加入stined inline,但我真的希望将样式分开.什么是在语义上区分各种标题/子标题/"正常"标签类的正确方法,以便它们可以在单独的XAML文档中正确设置样式?

谢谢!

Stu*_*art 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>
Run Code Online (Sandbox Code Playgroud)

然后在您的视图中,您只需调用资源并按如下方式使用它:

<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>
Run Code Online (Sandbox Code Playgroud)