2012-12-12 11 views
6

Ho il seguente codice per un Expander:WPF: La formattazione di un'etichetta

<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> 

I StaticResources sono definiti come segue (nel mio dizionario risorse):

<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> 

C'è un modo per definire una voce di dizionario (o qualcos'altro) che gestirà lo stile Font per l'intestazione e le etichette in modo che non debba definire lo stesso carattere più e più volte (e cambiarlo solo in un punto, se voglio cambiare il carattere) ?

EDIT

ho trovato una soluzione (grazie a coloro che hanno postato) e sto usando il seguente stile per le voci Label 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> 

e l'attuazione in questo modo (applicandolo a StackPanel in modo che colpisce tutte le etichette):

<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> 

una cosa che non funziona anche se è il Label.Foreground. Il colore di primo piano rimane nero ma posso modificare il carattere, la dimensione o il peso tramite lo stile. Se sposto lo stile nella definizione Etichetta anche se il colore funziona. È un bug o esiste una proprietà diversa che imposterà il colore del carattere (in primo piano) delle etichette StackPanel.

+0

È possibile creare uno stile comune e applicarlo alle etichette. – ryadavilli

+0

Grazie. Cercavo la formattazione delle etichette e non lo stile. Ho trovato la risposta una volta ho cercato lo stile. – BrianKE

+0

@BrianKE aggiornato per includere StackPanel –

risposta

9

è possibile utilizzare un Style all'interno Window.Resources, e fare riferimento a questo stile usando BasedOn all'interno della sezione StackPanel.Resources. Ciò applicherà gli stili a tutte le etichette all'interno di quello 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> 
0

dichiarare la Grandezza caratteri e nome nella risorsa 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}"/> 
4

utilizzare uno stile:

<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> 

Qui le Style obiettivi tutti Label all'interno Expander.

+0

contrassegnato questo corretto come è più vicino a quello che volevo. Comunque ho avuto una domanda sull'applicazione dello stile allo StackPanel (per favore vedi la domanda originale modifica) – BrianKE

Problemi correlati