2012-06-29 18 views
9

Estoy tratando de obtener un estilo para aplicar otro estilo a los elementos de un tipo determinado. De manera similar a CSS donde se haríaWPF - Cómo crear un estilo que aplique estilos a los tipos secundarios

div a 
{ 
    background-color:red; 
} 

Para aplicar un fondo rojo a todos <un> elementos que están contenidos por <div> elementos.

Específicamente, estoy tratando de obtener todas las TableCells contenidas dentro de un TableRowGroup con un cierto estilo para cambiar sus bordes.

Tengo la siguiente solución donde cada estilo de celda se configura individualmente.

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

Esto claramente no es preferido ya que hincha la xaml cuando hay muchas células.

He intentado lo siguiente sin éxito.

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

Esto también no funciona por alguna razón, aunque es válido

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

Va a ser un par de grupos de filas, cada una con su propio estilo de celda y cada una contiene muchas células. Por favor dime que hay una mejor manera.

Respuesta

7

Actualización basada en su comentario

Basado en su comentario, creo que el problema podría ser resuelto fácilmente usando Style herencia. A continuación se muestra un ejemplo del uso 2 Estilos de celda diferentes en diferentes TableRowGroups:

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

Cada vez que desee definir un general Style que se centrará en todos los elementos de un cierto tipo, no se debe especificar una clave para ese estilo . Intente eliminar x: clave del estilo y todo debería funcionar correctamente, como este:

<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

Soy consciente de cómo funciona el atributo x: Key; tu ejemplo tampoco funciona. Observe que en mi ejemplo estaba aplicando "HeaderStyle" a RowGroups específicos (porque no quiero que todos los grupos de filas de la tabla tengan este estilo), por lo que el estilo todavía se está aplicando correctamente. – Slight

Cuestiones relacionadas