首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF:格式化标签

WPF:格式化标签
EN

Stack Overflow用户
提问于 2012-12-12 17:17:54
回答 3查看 43.2K关注 0票数 12

我有下面的扩展程序代码:

代码语言:javascript
复制
   <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的定义如下(在我的资源字典中):

代码语言:javascript
复制
<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标签项:

代码语言:javascript
复制
<!-- 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,使其影响到所有标签):

代码语言:javascript
复制
<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。前景颜色保持黑色,但我可以改变字体,大小或重量通过样式。如果我将样式移到标签定义中,尽管颜色是有效的。这是一个错误,还是有一个不同的属性将设置字体颜色(前景)的StackPanel标签。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-12 17:31:57

您可以在Window.Resources中使用Window.Resources,并在StackPanel.Resources部分中使用BasedOn引用此样式。这将将样式应用于该StackPanel中的所有标签。

代码语言:javascript
复制
<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>
票数 17
EN

Stack Overflow用户

发布于 2012-12-12 17:28:59

使用一种风格:

代码语言:javascript
复制
<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>

在这里,StyleExpander中的所有Label为目标。

票数 5
EN

Stack Overflow用户

发布于 2012-12-12 17:28:09

在资源文件中声明Fontsize和字体名称

代码语言:javascript
复制
<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>


<Label Content="{StaticResource companyLinksItemMessages}" 
      FontSize="{StaticResource BaseFontSize}" FontFamily="{StaticResource fntfam}"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13845124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档