Reputation: 4185
I have the following code for an 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>
The StaticResources are defined as follows (in my resource dictionary):
<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>
Is there a way to define a dictionary entry (or something else) that will handle the Font styling for the Header and Labels so that I don't have to define the same font over and over (and only change it in one place should I want to change the font)?
EDIT
I found a solution (thanks to those that posted) and am using the following Style for the StackPanel Label items:
<!-- 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>
and implementing it like this (applying it to the StackPanel so it affects all Labels):
<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>
One thing that does not work though is the Label.Foreground. The foreground color remains black but I can change the font, size or weight via the style. If I move the style into the Label definition though the color works. Is this a bug or is there a different property that will set the font color (foreground) of the StackPanel Labels.
Upvotes: 12
Views: 49139
Reputation: 17402
You can use a Style
within Window.Resources
, and refer to this style using BasedOn within the StackPanel.Resources
section. This will apply the styles to all labels inside that 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>
Upvotes: 18
Reputation: 17083
Use a Style:
<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>
Here the Style
targets all Label
within Expander
.
Upvotes: 5
Reputation: 21873
Declare the Fontsize and Font Name in the Resource file
<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>
<Label Content="{StaticResource companyLinksItemMessages}"
FontSize="{StaticResource BaseFontSize}" FontFamily="{StaticResource fntfam}"/>
Upvotes: 0