WPF-如何创建将样式应用于子类型的样式
我试图获得一种样式,将另一种样式应用于特定类型的元素。类似于CSS,您可以在其中执行WPF-如何创建将样式应用于子类型的样式,wpf,xaml,styles,tablecell,Wpf,Xaml,Styles,Tablecell,我试图获得一种样式,将另一种样式应用于特定类型的元素。类似于CSS,您可以在其中执行 diva { 背景色:红色; } 将红色背景应用于元素包含的所有元素 具体地说,我试图使包含在具有特定样式的TableRowGroup中的所有TableCell的边框都发生更改 我有以下解决方案,其中每个单元格样式都是单独设置的 描述 数量 这显然不是首选,因为当有许多细胞时,它会使xaml膨胀 我试过以下方法,但没有成功 虽然这是有效的,但出于某种原因,它也不起作用 将有几个行组,每个行组有
diva
{
背景色:红色;
}
将红色背景应用于元素包含的所有元素
具体地说,我试图使包含在具有特定样式的TableRowGroup中的所有TableCell的边框都发生更改
我有以下解决方案,其中每个单元格样式都是单独设置的
描述
数量
这显然不是首选,因为当有许多细胞时,它会使xaml膨胀
我试过以下方法,但没有成功
虽然这是有效的,但出于某种原因,它也不起作用
将有几个行组,每个行组有自己的单元格样式,每个行组包含许多单元格。请告诉我有更好的方法。根据您的评论更新 根据您的评论,我相信您的问题可以通过
Style
继承轻松解决。下面是在不同TableRowGroup上使用两种不同单元格样式的示例:
<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>
我知道x:Key属性是如何工作的;你的例子也不起作用。请注意,在我的示例中,我将“HeaderStyle”应用于特定的行组(因为我不希望所有表行组都具有此样式),因此仍然可以正确应用该样式。
<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>