WPF:格式化标签

时间:2012-12-12 17:17:55

标签: wpf fonts resourcedictionary

我有一个Expander的代码:

   <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,因此它会影响所有标签):

<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标签的字体颜色(前景)。

3 个答案:

答案 0 :(得分:11)

您可以在Style中使用Window.Resources,并使用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>

答案 1 :(得分:4)

使用样式:

<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定位Label内的所有Expander

答案 2 :(得分: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}"/>