Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Formatting a label

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.

like image 420
BrianKE Avatar asked Dec 12 '12 17:12

BrianKE


People also ask

What is the difference between TextBlock and label in WPF?

Labels usually support single line text output while the TextBlock is intended for multiline text display. For example in wpf TextBlock has a property TextWrapping which enables multiline input; Label does not have this.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.

How do I center text in WPF?

If you want to center each line, use a TextBlock instead, and set TextAlignment="Center" .


1 Answers

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>
like image 120
d.moncada Avatar answered Oct 03 '22 13:10

d.moncada