WPF - Como criar um estilo que aplica estilos a tipos de filhos
Estou tentando obter um estilo para aplicar outro estilo a elementos de um determinado tipo. Semelhante ao CSS onde você faria
div a
{
background-color:red;
}
Para aplicar um plano de fundo vermelho a todos os elementos <a> contidos pelos elementos <div>.
Especificamente, estou tentando fazer com que todos os TableCells contidos em um TableRowGroup com um determinado estilo tenham suas bordas alteradas.
Eu tenho a seguinte solução, onde cada estilo de célula é definido 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>
Isto claramente não é preferido, pois incha o xaml quando há muitas células.
Eu tentei o seguinte sem sucesso.
<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>
Isso também não funciona por algum motivo, embora seja 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>
Haverá alguns grupos de linhas, cada um com seu próprio estilo de célula e cada um contendo muitas células. Por favor me diga que há um jeito melhor.