WPF:格式化标签

12

我有以下用于展开器的代码:

   <Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
              FontSize="18" FontFamily="Calibri" FontWeight="Bold">
        <StackPanel>
            <Label Content="{StaticResource companyLinksItemSummary}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
            <Label Content="{StaticResource companyLinksItemInfo}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
            <Label Content="{StaticResource companyLinksItemIssues}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
            <Label Content="{StaticResource companyLinksItemMessages}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
        </StackPanel>   
    </Expander>

StaticResources 在我的资源字典中定义如下:

<sys:String x:Key="companyLinksHeader">company</sys:String>
<sys:String x:Key="companyLinksItemSummary">summary</sys:String>
<sys:String x:Key="companyLinksItemInfo">info</sys:String>
<sys:String x:Key="companyLinksItemIssues">issues</sys:String>
<sys:String x:Key="companyLinksItemMessages">messages</sys:String>

有没有一种方法可以定义字典条目(或其他内容),以处理标题和标签的字体样式,这样我就不必一遍又一遍地定义相同的字体(如果我想要更改字体,则只需在一个地方更改即可)?

编辑

我找到了一个解决方案(感谢那些发帖的人),并正在使用以下样式来处理StackPanel标签项:

<!-- Expander Items text style -->
<Style x:Key="expanderItemsTextStyle">
    <Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
    <Setter Property="Label.FontWeight" Value="Normal"></Setter>
    <Setter Property="Label.FontSize" Value="14"></Setter>
    <Setter Property="Label.Foreground" Value="Aqua"></Setter>
</Style>

并像这样实现它(将其应用于StackPanel,以便影响所有Label):

<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
          Style="{StaticResource expanderHeaderTextStyle}">
    <StackPanel Style="{StaticResource expanderItemsTextStyle}">
        <Label Content="{StaticResource companyLinksItemSummary}"/>
        <Label Content="{StaticResource companyLinksItemInfo}" />
        <Label Content="{StaticResource companyLinksItemIssues}" />
        <Label Content="{StaticResource companyLinksItemMessages}" />
    </StackPanel>   
</Expander>

然而有一点不起作用,那就是Label.Foreground。前景颜色仍然是黑色,但我可以通过样式更改字体、大小或重量。如果我将样式移动到Label定义中,颜色就可以正常工作。这是一个错误吗?还是有其他属性可以设置StackPanel标签的字体颜色(前景)。


您可以创建一个通用的样式并将其应用于标签。 - Ravi Y
谢谢。我在寻找标签格式而不是样式。当我搜索样式时,找到了答案。 - BrianKE
@BrianKE 更新,包括 StackPanel。 - d.moncada
在这里还发现了一个类似的问题/答案:https://dev59.com/v2445IYBdhLWcg3wZJiw - BrianKE
3个回答

17

您可以在Window.Resources之下使用一个Style,然后在StackPanel.Resources部分中使用BasedOn来引用此样式。这将适用于该StackPanel内的所有标签。

<Window>
    <Window.Resources>
        <Style x:Key="myLabelStyle" TargetType="{x:Type Label}">
            <Setter Property="FontSize" Value="14" />
            <Setter Property="FontFamily" Value="Calibri" />
            <Setter Property="FontWeight" Value="Bold" />
         </Style>
    </Window.Resources>
    <Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
              FontSize="18" FontFamily="Calibri" FontWeight="Bold">
        <StackPanel>
            <StackPanel.Resources>
                <Style BasedOn="{StaticResource myLabelStyle}" TargetType="{x:Type Label}" />
            </StackPanel.Resources>
            <Label Content="{StaticResource companyLinksItemSummary}" />
            <Label Content="{StaticResource companyLinksItemInfo}" />
            <Label Content="{StaticResource companyLinksItemIssues}" />
            <Label Content="{StaticResource companyLinksItemMessages}" />
        </StackPanel>
    </Expander>
</Window>

5

使用样式:

<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
          FontSize="18" FontFamily="Calibri" FontWeight="Bold">
    <Expander.Resources>
        <Style TargetType="Label">
            <Setter Property="FontSize" Value="14" />
            <Setter Property="FontFamily" Value="Calibri" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </Expander.Resources>
    <StackPanel>
        <Label Content="{StaticResource companyLinksItemSummary}" />
        <Label Content="{StaticResource companyLinksItemInfo}" />
        <Label Content="{StaticResource companyLinksItemIssues}" />
        <Label Content="{StaticResource companyLinksItemMessages}" />
    </StackPanel>
</Expander>

这里的Style针对的是Expander中的所有Label

这个问题我标记为正确,因为它接近我想要的答案。但是我确实有一个关于将样式应用到 StackPanel 的问题(请查看我的编辑过的原始问题)。 - BrianKE

0
在资源文件中声明字体大小和字体名称。
<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>


<Label Content="{StaticResource companyLinksItemMessages}" 
      FontSize="{StaticResource BaseFontSize}" FontFamily="{StaticResource fntfam}"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接