2012-12-12 13 views
6

のフォーマット:WPF:私はパンダのために、次のコードを持っているラベル

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

を定義する方法はありますヘッダーとラベルのフォントスタイルを処理して、同じフォントを何度も定義する必要がなくなるようにする辞書エントリ(または何か他のもの) ?

EDIT

は、私は解決策(掲示するもののおかげで)見つけ、の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はすべてのラベルに影響を与えます)。

<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はうまくいきません。フォアグラウンドカラーは黒のままですが、スタイルを使用してフォント、サイズ、またはウェイトを変更できます。色は動作しますが、スタイルをラベル定義に移動するとどうなりますか?これはバグか、StackPanelラベルのフォント色(フォアグラウンド)を設定する別のプロパティです。

+0

共通のスタイルを作成し、ラベルに適用することができます。 – ryadavilli

+0

ありがとうございます。ラベルの書式を検索していてスタイリングしていなかった私がスタイルを探したら答えを見つけました。 – BrianKE

+0

@BrianKEがStackPanelをインクルードするように更新されました –

答えて

9

StyleWindow.Resources以内にあり、セクション内のBasedOnを使用してこのスタイルを参照してください。これにより、その中のすべてのラベルにスタイルが適用されます。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

リソースファイルにフォントサイズやフォント名を宣言し

<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

スタイル使用します。ここではStyleターゲットExpander内のすべてのLabel

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

を。

+0

それは私が望むものに近いほど正しいものです。しかし、私はStackPanelにスタイルを適用することについての質問がありました(元の質問の編集を参照してください) – BrianKE

関連する問題