WPF - 如何创建将样式应用于子类型的样式

时间:2012-06-29 19:44:53

标签: wpf xaml styles tablecell

我正在尝试使用另一种样式将样式应用于某种类型的元素。类似于你要做的CSS

div a  
{  
    background-color:red;  
}

将红色背景应用于所有< a> < div>包含的元素元件。

具体来说,我试图让TableRowGroup中包含的所有TableCell都具有某种样式,以便更改其边框。

我有以下解决方案,其中每个单元格样式都是单独设置的。

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

这显然不是优选的,因为当有许多细胞时,它会使xaml膨胀。

我尝试了以下但没有成功。

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

由于某些原因,这也不起作用,但有效

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

将会有一些行组,每个行都有自己的单元格样式,每个行都包含许多单元格。请告诉我有更好的方法。

1 个答案:

答案 0 :(得分:7)

根据您的评论进行更新

根据您的评论,我相信您的问题可以使用Style继承轻松解决。以下是在不同TableRowGroups上使用2种不同Cell样式的示例:

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

每当您想要定义一个将定位某种类型的所有元素的一般Style时,您就不能为该样式指定一个Key。尝试从样式中删除x:Key,一切都应该正常工作,如下所示:

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