2012-12-12 48 views
6

Tôi đã đoạn mã sau cho một Expander:WPF: Định dạng nhãn

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

Các StaticResources được định nghĩa như sau (trong từ điển tài nguyên của tôi):

<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ó cách nào để xác định một mục từ điển (hoặc thứ gì đó khác) sẽ xử lý kiểu phông chữ cho Tiêu đề và Nhãn để tôi không phải định nghĩa cùng một phông chữ giống nhau (và chỉ thay đổi nó ở một nơi nếu tôi muốn thay đổi phông chữ) ?

EDIT

Tôi tìm thấy một giải pháp (nhờ những người đăng) và đang sử dụng Style sau cho StackPanel Label mục:

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

và thực hiện nó như thế này (áp dụng nó để StackPanel để nó ảnh hưởng đến tất cả các Nhãn):

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

Một điều không hoạt động là Label.Foreground. Màu nền trước vẫn màu đen nhưng tôi có thể thay đổi phông chữ, kích thước hoặc trọng lượng thông qua kiểu. Nếu tôi di chuyển phong cách vào định nghĩa Nhãn mặc dù màu sắc hoạt động. Đây có phải là lỗi hay không có thuộc tính khác sẽ đặt màu phông chữ (tiền cảnh) của Nhãn StackPanel.

+0

Bạn có thể tạo kiểu chung và áp dụng cho nhãn. – ryadavilli

+0

Cảm ơn bạn. Đã tìm kiếm định dạng nhãn và không tạo kiểu. Tìm thấy câu trả lời khi tôi tìm kiếm phong cách. – BrianKE

+0

@BrianKE được cập nhật để bao gồm StackPanel –

Trả lời

9

Bạn có thể sử dụng Style trong phạm vi Window.Resources và tham khảo kiểu này bằng cách sử dụng BasedOn trong phần StackPanel.Resources. Điều này sẽ áp dụng kiểu cho tất cả các nhãn bên trong đó 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

Khai báo Cỡ chữ và Tên phông chữ trong Resource nộp

<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

Sử dụng Kiểu:

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

Ở đây Style mục tiêu tất cả Label trong Expander.

+0

Đã đánh dấu trang này đúng như nó gần với những gì tôi muốn. Tuy nhiên tôi đã có câu hỏi về việc áp dụng kiểu cho StackPanel (vui lòng xem bản chỉnh sửa câu hỏi gốc) – BrianKE

Các vấn đề liên quan