2012-06-29 18 views
9

Próbuję uzyskać styl do zastosowania innego stylu do elementów określonego typu. Podobny do CSS, gdzie byś zrobićWPF - Jak utworzyć styl, który stosuje style do typów potomnych

div a 
{ 
    background-color:red; 
} 

Aby zastosować czerwone tło do wszystkich <a> elementów, które są zawarte przez <div> elementów.

W szczególności staram się, aby wszystkie TableCells zawarte w TableRowGroup o określonym stylu zmieniły ich granice.

Mam następujące rozwiązanie, w którym każdy styl komórki jest ustalany indywidualnie.

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

Oczywiście nie jest to preferowane, ponieważ rozciąga się na Xaml, gdy istnieje wiele komórek.

Próbowałem następujące rzeczy bez powodzenia.

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

To także nie działa z jakiegoś powodu, choć jest ważny

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

Nie będzie kilka grup wierszy każdy z własnym stylu komórki i zawiera wiele komórek. Powiedz mi, że jest lepszy sposób.

Odpowiedz

7

Aktualizacja oparta na komentarz

podstawie Twojego komentarza, wierzę, problem może być łatwo rozwiązane za pomocą Style dziedziczenia. Poniżej znajduje się przykład użycia 2 różnych stylów komórek na różnych 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> 

Gdy chcesz określić ogólny Style które będą kierowane na wszystkie elementy określonego typu, nie musi określać kluczem do tego stylu . Spróbuj usunąć x: Key ze stylu i wszystko powinno działać poprawnie, tak jak to:

<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

Jestem świadomy tego, jak działa atrybut x: Key; twój przykład też nie działa. Zauważ, że w moim przykładzie zastosowałem "HeaderStyle" do określonych grup wierszy (ponieważ nie chcę, aby wszystkie grupy wierszy tabeli miały ten styl), aby styl był nadal poprawnie stosowany. – Slight

Powiązane problemy