2012-12-12 6 views
6

Ich habe den folgenden Code für einen Expander:WPF: Formatieren einer Beschriftung

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

Die StaticResources sind wie folgt definiert (in meinem Ressource-Wörterbuch):

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

Gibt es eine Möglichkeit zu definieren, ein Wörterbucheintrag (oder etwas anderes), das den Font-Stil für den Header und die Labels übernimmt, so dass ich nicht immer wieder die gleiche Schriftart definieren muss (und sie nur an einer Stelle ändern muss, wenn ich die Schriftart ändern möchte) ?

EDIT

fand ich eine Lösung (Dank an diejenigen, die gebucht) und bin die folgende Art für die Stackpanel Label-Elemente mit:

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

und es so Umsetzung (Anwendung auf das StackPanel, so dass es alle Labels betrifft):

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

Eine Sache, die nicht funktioniert, ist jedoch das Label.Foreground. Die Vordergrundfarbe bleibt schwarz, aber ich kann die Schriftart, Größe oder das Gewicht über den Stil ändern. Wenn ich den Stil in die Label-Definition verschiebe, funktioniert die Farbe. Ist das ein Fehler oder gibt es eine andere Eigenschaft, die die Schriftfarbe (Vordergrund) der StackPanel-Labels festlegt?

+0

Sie können einen gemeinsamen Stil erstellen und auf die Beschriftungen anwenden. – ryadavilli

+0

Danke. War nach Etikettenformatierung und nicht nach Styling gesucht. Ich habe die Antwort gefunden, als ich nach Stil gesucht habe. – BrianKE

+0

@BrianKE aktualisiert mit StackPanel –

Antwort

9

Sie können Style innerhalb von Window.Resources verwenden, und beziehen sich auf diesen Stil mit innerhalb der StackPanel.Resources Abschnitt. Dadurch werden die Stile auf alle Etiketten in diesem StackPanel angewendet.

<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

Deklarieren Sie die Schriftgröße und Schriftnamen in der Ressourcen-Datei

<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

Verwenden Sie einen 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> 

Hier die Style Ziele alle Label innerhalb Expander.

+0

Diese markiert so korrekt wie es ist, was ich wollte. Wie auch immer, ich hatte eine Frage bezüglich der Anwendung des Stils auf das StackPanel (siehe Original-Frage) – BrianKE

Verwandte Themen