WPF - 如何创建将样式应用于子类型的样式 [英] WPF - How to create a style that applies styles to child types

查看:33
本文介绍了WPF - 如何创建将样式应用于子类型的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一种样式以将另一种样式应用于某种类型的元素.类似于 CSS 中你会做的

I'm trying to get a style to apply another style to elements of a certain type. Similar to CSS where you would do

div a  
{  
    background-color:red;  
}

将红色背景应用到所有 <div> 包含的元素元素.

To apply a red background to all <a> elements that are contained by <div> elements.

具体来说,我试图让包含在具有特定样式的 TableRowGroup 中的所有 TableCell 的边框发生变化.

Specifically, I'm trying to get all TableCells contained within a TableRowGroup with a certain style to have their borders changed.

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

I have the following solution where each cell style is set individually.

<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 膨胀.

This is clearly not preferred as it bloats the xaml when there are many cells.

我尝试了以下方法但没有成功.

I've tried the following with no success.

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

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

This also doesn't work for some reason, though is valid

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

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

There's going to be a few row groups each with their own cell style and each containing many cells. Please tell me there's a better way.

推荐答案

根据您的评论更新

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

Based on your comment, I believe your problem could be easily solved using Style inheritance. Below is an example of using 2 different Cell Styles on different 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>

每当您想定义一个通用的 Style 来定位特定类型的所有元素时,您都不能为该样式指定 Key.尝试从样式中删除 x:Key,一切都应该正常工作,如下所示:

Whenever you want to define a general Style that will target all of the elements of a certain type, you must not specify a Key for that style. Try removing x:Key from the Style and everything should work properly, like this:

<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 - 如何创建将样式应用于子类型的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆