WPF:格式化标签

Bri*_*nKE 8 wpf fonts resourcedictionary

我有一个扩展器的代码如下:

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

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

有没有办法定义一个字典条目(或其他东西)来处理标题和标签的字体样式,这样我就不必一遍又一遍地定义相同的字体(并且只在一个地方更改它应该我想要改变字体)?

编辑

我找到了一个解决方案(感谢那些发布的解决方案)并且我正在使用以下样式作为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>
Run Code Online (Sandbox Code Playgroud)

并像这样实现它(将它应用于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>
Run Code Online (Sandbox Code Playgroud)

但有一点不起作用的是Label.Foreground.前景色仍为黑色,但我可以通过样式更改字体,大小或重量.如果我将样式移动到Label定义中,尽管颜色有效.这是一个错误还是有一个不同的属性,将设置StackPanel标签的字体颜色(前景).

d.m*_*ada 12

您可以使用Style内部Window.Resources,并在该部分中使用BasedOn引用此样式StackPanel.Resources.这会将样式应用于其中的所有标签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>
Run Code Online (Sandbox Code Playgroud)


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

这里的Style目标都LabelExpander.