2012-06-29 10 views
9

Sto cercando di ottenere uno stile per applicare un altro stile agli elementi di un certo tipo. Simile a CSS in cui si farebbeWPF - Come creare uno stile che applica gli stili ai tipi di figlio

div a 
{ 
    background-color:red; 
} 

Per applicare uno sfondo rosso a tutti i <un> elementi che sono contenuti da <div> elementi.

In particolare, sto cercando di ottenere tutte le TableCell contenute in un TableRowGroup con uno stile specifico per modificare i bordi.

Ho la seguente soluzione in cui ogni stile di cella è impostato 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> 

Questo non è chiaramente preferito poiché gonfia lo xaml quando ci sono molte celle.

Ho provato il seguente senza successo.

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

Anche questo non funziona per qualche ragione, anche se è valida

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

Ci sara 'un paio di gruppi di righe ognuno con il proprio stile e le cellule ognuna delle quali contiene molte cellule. Per favore dimmi che c'è un modo migliore.

risposta

7

aggiornamento sulla base di un commento

Sulla base di tuo commento, credo che il problema potrebbe essere facilmente risolto utilizzando Style eredità. Di seguito è riportato un esempio di utilizzo 2 diversi stili di cella su diversi 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> 

Ogni volta che si desidera definire un generale Style che si rivolgerà tutti gli elementi di un certo tipo, non è necessario specificare una chiave per quello stile . Prova a rimuovere x: Chiave dallo stile e tutto dovrebbe funzionare correttamente, come questo:

<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

Sono a conoscenza di come funziona l'attributo x: Key; il tuo esempio non funziona neanche. Si noti che nel mio esempio stavo applicando "HeaderStyle" a RowGroup specifici (perché non voglio che tutti i gruppi di righe tabella abbiano questo stile), quindi lo stile viene ancora applicato correttamente. – Slight

Problemi correlati