2012-06-29 16 views
9

Tôi đang cố tạo kiểu để áp dụng kiểu khác cho các thành phần của một loại nhất định. Tương tự như CSS, nơi bạn sẽ làmWPF - Cách tạo kiểu áp dụng kiểu cho kiểu con

div a 
{ 
    background-color:red; 
} 

Để áp dụng một nền đỏ cho tất cả < một > yếu tố được chứa bởi <div> yếu tố.

Cụ thể, tôi đang cố gắng để có được tất cả TableCells chứa trong TableRowGroup với một phong cách nhất định để thay đổi đường viền của chúng.

Tôi có giải pháp sau đây trong đó mỗi kiểu ô được đặt riêng lẻ.

<Table> 
    <Table.Columns> 
     <TableColumn/> 
     <TableColumn/> 
    </Table.Columns> 

    <Table.Resources> 
     <Style x:Key="HeaderStyle" TargetType="{x:Type TableRowGroup}"> 
      <Setter Property="FontWeight" Value="Normal"/> 
      <Setter Property="FontSize" Value="12"/> 
     </Style> 

     <Style x:Key="HeaderCellStyle" TargetType="{x:Type TableCell}"> 
      <Setter Property="BorderThickness" Value="0,1,0,1" /> 
      <Setter Property="BorderBrush" Value="Black" /> 
     </Style> 
    </Table.Resources> 

    <TableRowGroup Name="TableColumnHeaders" Style="{StaticResource HeaderStyle}"> 
     <TableRow> 
      <TableCell Style="{StaticResource HeaderCellStyle}"> 
       <Paragraph> 
        Description 
       </Paragraph> 
      </TableCell> 
      <TableCell Style="{StaticResource HeaderCellStyle}"> 
       <Paragraph> 
        Amount 
       </Paragraph> 
      </TableCell> 
     </TableRow> 
    </TableRowGroup> 
</Table> 

Điều này rõ ràng không được ưa thích vì nó phồng lên xaml khi có nhiều ô.

Tôi đã thử những điều sau đây mà không thành công.

<Table.Resources> 
    <Style x:Key="HeaderStyle" TargetType="{x:Type TableRowGroup}"> 
     <Style.Resources> 
      <Style TargetType="{x:Type TableCell}"> 
       <Setter Property="BorderThickness" Value="0,1,0,1" /> 
       <Setter Property="BorderBrush" Value="Black" /> 
      </Style> 
     </Style.Resources> 
     <Setter Property="FontWeight" Value="Normal"/> 
     <Setter Property="FontSize" Value="12"/> 
    </Style> 
</Table.Resources> 

này cũng không làm việc cho một số lý do, mặc dù có giá trị

<Table.Resources> 
    <Style x:Key="HeaderStyle" TargetType="{x:Type TableRowGroup}"> 
     <Setter Property="FontWeight" Value="Normal"/> 
     <Setter Property="FontSize" Value="12"/> 
     <Setter Property="TableCell.BorderThickness" Value="0,1,0,1" /> 
     <Setter Property="TableCell.BorderBrush" Value="Black" /> 
    </Style> 
</Table.Resources> 

Có sẽ là một vài nhóm hàng mỗi với phong cách di động của riêng mình và mỗi có chứa nhiều tế bào. Hãy cho tôi biết có cách tốt hơn.

Trả lời

7

Cập nhật dựa trên nhận xét của bạn

Dựa trên nhận xét của bạn, tôi tin rằng vấn đề của bạn có thể dễ dàng giải quyết bằng Style thừa kế. Dưới đây là một ví dụ của việc sử dụng 2 Cell Styles khác nhau trên TableRowGroups khác nhau:

<Table> 
    <Table.Resources> 

     <Style x:Key="HeaderCellStyle" TargetType="{x:Type TableCell}"> 
      <Setter Property="BorderThickness" Value="0,1,0,1" /> 
      <Setter Property="BorderBrush" Value="Black" /> 
      <Setter Property="TextAlignment" Value="Center" /> 
      <Setter Property="FontStyle" Value="Italic" /> 
      <Setter Property="Padding" Value="5" /> 
     </Style> 

     <Style x:Key="FooterCellStyle" BasedOn="{StaticResource HeaderCellStyle}" TargetType="{x:Type TableCell}"> 
      <Setter Property="Background" Value="AliceBlue" /> 
      <Setter Property="TextAlignment" Value="Right" /> 
      <Setter Property="FontWeight" Value="Bold" /> 
     </Style> 

     <Style x:Key="HeaderTableRowGroupStyle" TargetType="{x:Type TableRowGroup}"> 
      <Style.Resources> 
       <Style BasedOn="{StaticResource HeaderCellStyle}" TargetType="{x:Type TableCell}" /> 
      </Style.Resources> 
     </Style> 

     <Style x:Key="FooterTableRowGroupStyle" TargetType="{x:Type TableRowGroup}"> 
      <Style.Resources> 
       <Style BasedOn="{StaticResource FooterCellStyle}" TargetType="{x:Type TableCell}" /> 
      </Style.Resources> 
     </Style> 

    </Table.Resources> 
    <Table.Columns> 
     <TableColumn /> 
     <TableColumn /> 
     <TableColumn /> 
     <TableColumn /> 
    </Table.Columns> 

    <!-- This TableRowGroup hosts a header row for the table. --> 
    <TableRowGroup Style="{StaticResource HeaderTableRowGroupStyle}"> 
     <TableRow> 
      <TableCell /> 
      <TableCell> 
       <Paragraph>Gizmos</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>Thingamajigs</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>Doohickies</Paragraph> 
      </TableCell> 
     </TableRow> 
    </TableRowGroup> 

    <!-- This TableRowGroup hosts the main data rows for the table. --> 
    <TableRowGroup> 
     <TableRow> 
      <TableCell> 
       <Paragraph>Blue</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>1</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>2</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>3</Paragraph> 
      </TableCell> 
     </TableRow> 
     <TableRow> 
      <TableCell> 
       <Paragraph>Red</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>1</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>2</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>3</Paragraph> 
      </TableCell> 
     </TableRow> 
     <TableRow> 
      <TableCell> 
       <Paragraph>Green</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>1</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>2</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>3</Paragraph> 
      </TableCell> 
     </TableRow> 
    </TableRowGroup> 

    <!-- This TableRowGroup hosts a footer row for the table. --> 
    <TableRowGroup Style="{StaticResource FooterTableRowGroupStyle}"> 
     <TableRow> 
      <TableCell> 
       <Paragraph>Totals</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>3</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>6</Paragraph> 
      </TableCell> 
      <TableCell> 
       <Paragraph>9</Paragraph> 
      </TableCell> 
     </TableRow> 
    </TableRowGroup> 
</Table> 

Bất cứ khi nào bạn muốn xác định một vị tướng Style mà sẽ nhắm mục tiêu tất cả các yếu tố của một loại nhất định, bạn không phải chỉ định một chính cho phong cách . Hãy thử xóa x: Khóa khỏi Kiểu và mọi thứ sẽ hoạt động bình thường, như sau:

<Table.Resources> 
    <Style TargetType="{x:Type TableRowGroup}"> 
     <Setter Property="FontWeight" Value="Normal"/> 
     <Setter Property="FontSize" Value="12"/> 
     <Setter Property="TableCell.BorderThickness" Value="0,1,0,1" /> 
     <Setter Property="TableCell.BorderBrush" Value="Black" /> 
    </Style> 
</Table.Resources> 
+0

Tôi biết cách x: Thuộc tính khóa hoạt động; ví dụ của bạn không hoạt động. Lưu ý rằng trong ví dụ của tôi, tôi đã áp dụng "HeaderStyle" cho các RowGroups cụ thể (vì tôi không muốn tất cả các nhóm trong bảng có kiểu này) nên kiểu dáng vẫn đang được áp dụng đúng cách. – Slight