WPF - 如何创建应用于样式子类型风格(WPF - How to create a style

2019-07-30 04:18发布

我试图让一个风格到另一个风格适用于某一特定类型的元素。 类似CSS,你会怎么做

div a  
{  
    background-color:red;  
}

要应用红色背景到由<div>元素包含<a>元。

具体来说,我试图让具有一定风格TableRowGroup内载有其边界的所有TableCells改变。

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

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

还有的将是每几行组用自己的单元格样式和每个包含许多细胞。 请告诉我有一个更好的办法。

Answer 1:

更新根据您的评论

基于您的评论,我相信你的问题可以用迎刃而解Style继承。 下面是一个使用在不同TableRowGroups 2种不同单元格样式的一个例子:

<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 ,将针对所有某种类型的元素,你不能指定该风格的关键。 尝试删除X:主要从风格,一切都应该正常工作,就像这样:

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


文章来源: WPF - How to create a style that applies styles to child types