如何在WPF/Silverlight/XAML中设置标题标签的样式?

如何在WPF/Silverlight/XAML中设置标题标签的样式?,wpf,silverlight,xaml,Wpf,Silverlight,Xaml,这似乎很简单,但我很难找到答案 我有一个WPF窗口,需要使用不同区域的标题和子标题。当然,我可以为每个元素包含stling内联,但我真的希望保持样式独立。在语义上区分不同的标题/子标题/“普通”标签类的正确方法是什么,以便在单独的XAML文档中正确设置它们的样式 谢谢 您只需在资源字典中为所需的每个“标签类”设置样式,如下所示: <Style x:Key="heading" TargetType="Label"> <Setter Property="FontSize"

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

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


谢谢

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

<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>

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

<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>

您可以从资源中手动设置样式,或者创建一组继承
TextBlock
并具有不同自动样式的自定义控件