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



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

我有一个WPF窗口,要求使用不同领域的标题和副标题。当然,我可以为每个元素包含stlying内联,但我真的想把样式分开。从语义上区分各种标题/副标题/"正常"标签类的正确方法是什么,以便在单独的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>

最新更新