我有以下用于展开器的代码:
<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标签的字体颜色(前景)。