Table in WPF

本文介绍了一种使用WPF实现类似HTML表格布局的方法,并详细展示了如何通过自定义样式来改变表格元素的外观,包括背景颜色、字体大小等属性。
<Window x:Class="DataGrid.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window4" Height="300" Width="980">
    <Window.Resources>
        <SolidColorBrush x:Key="ProductNameBackgroundBrush" Color="#87B7FF" />
        <SolidColorBrush x:Key="SubjectAreaBackgroundBrush" Color="#D99795" />
        <SolidColorBrush x:Key="ExceptionHeaderBackgroundBrush" Color="#E6B9B8" />
        <SolidColorBrush x:Key="ExceptionCellBackgroundBrush" Color="#F2DDDC" />
        <Style x:Key="ProductNameStyle" TargetType="{x:Type TableCell}">
            <Setter Property="Background" Value="{StaticResource ProductNameBackgroundBrush}" />
            <Setter Property="FontFamily" Value="Trebuchet MS" />
            <Setter Property="FontWeight" Value="Light" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="3,18" />
            <Setter Property="Cursor" Value="Arrow" />
            <Setter Property="TextAlignment" Value="Center" />
        </Style>
        <Style x:Key="ProductNameStyle2" TargetType="{x:Type TableCell}">
            <Setter Property="Background" Value="{StaticResource ProductNameBackgroundBrush}" />
            <Setter Property="FontFamily" Value="Trebuchet MS" />
            <Setter Property="FontWeight" Value="Light" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Cursor" Value="Arrow" />
            <Setter Property="Padding" Value="3,6" />
            <Setter Property="TextAlignment" Value="Center" />
        </Style>
        <Style x:Key="SubjectAreaStyle" TargetType="{x:Type TableCell}">
            <Setter Property="Background" Value="{StaticResource SubjectAreaBackgroundBrush}" />
            <Setter Property="FontFamily" Value="Trebuchet MS" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontSize" Value="15" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="3,6,0,6" />
        </Style>
        <Style x:Key="ExceptionHeaderStyle" TargetType="{x:Type TableCell}">
            <Setter Property="Background" Value="{StaticResource ExceptionHeaderBackgroundBrush}" />
            <Setter Property="FontFamily" Value="Trebuchet MS" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontSize" Value="15" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="3,6,0,6" />
        </Style>
        <Style x:Key="ExceptionCellStyle" TargetType="{x:Type TableCell}">
            <Setter Property="Background" Value="{StaticResource ExceptionCellBackgroundBrush}" />
            <Setter Property="FontFamily" Value="Trebuchet MS" />
            <Setter Property="FontWeight" Value="Normal" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="3,6,0,6" />
        </Style>
    </Window.Resources>
    <Grid SnapsToDevicePixels="True">
        <FlowDocumentScrollViewer SnapsToDevicePixels="True"  VerticalScrollBarVisibility="Disabled" Cursor="Arrow">
            <FlowDocument  ColumnWidth="40" FontSize="14" FontFamily="Georgia">
                <Table Background="Black"  CellSpacing="1" BorderThickness="1" BorderBrush="Black" >
                    <TableRowGroup>
                        <TableRow >
                            <TableCell ColumnSpan="1" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0" Padding="0,18,0,0" Style="{StaticResource ProductNameStyle}">
                                <Paragraph>
                                    XX
                                </Paragraph>
                            </TableCell>
                            <TableCell ColumnSpan="4" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Paragraph>
                                    XXX
                                </Paragraph>
                            </TableCell>
                            <TableCell ColumnSpan="4" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Paragraph>
                                    XXXX
                                </Paragraph>
                            </TableCell>
                            <TableCell ColumnSpan="6" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Paragraph>
                                    XXXXX
                                </Paragraph>
                            </TableCell>
                            <TableCell Padding="0" ColumnSpan="10" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Table Margin="0" Background="Black"  CellSpacing="1" BorderThickness="0" BorderBrush="Red" >
                                    <TableRowGroup>
                                        <TableRow >
                                            <TableCell ColumnSpan="5"  BorderThickness="0"  Style="{StaticResource ProductNameStyle2}">
                                                <Paragraph> CCCCCCC</Paragraph>
                                            </TableCell>
                                        </TableRow>
                                        <TableRow >
                                            <TableCell  BorderThickness="0"  Style="{StaticResource ProductNameStyle2}">
                                                <Paragraph>ZZZZZZ</Paragraph>
                                            </TableCell>
                                            <TableCell  BorderThickness="0"  Style="{StaticResource ProductNameStyle2}">
                                                <Paragraph>XXX</Paragraph>
                                            </TableCell>
                                            <TableCell  BorderThickness="0"  Style="{StaticResource ProductNameStyle2}">
                                                <Paragraph>XXX</Paragraph>
                                            </TableCell>
                                            <TableCell  BorderThickness="0"  Style="{StaticResource ProductNameStyle2}">
                                                <Paragraph>XXX</Paragraph>
                                            </TableCell>
                                            <TableCell  BorderThickness="0"  Style="{StaticResource ProductNameStyle2}">
                                                <Paragraph>XXX</Paragraph>
                                            </TableCell>
                                        </TableRow>
                                    </TableRowGroup>
                                </Table>
                            </TableCell>
                            <TableCell ColumnSpan="5" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Paragraph>
                                    XXXXXX
                                </Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow >
                            <TableCell ColumnSpan="1" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Paragraph>
                                    XXXXXXXX
                                </Paragraph>
                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="4" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="4" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="6" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="2" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                            <TableCell ColumnSpan="5" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">


                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
            </FlowDocument>
        </FlowDocumentScrollViewer>
        <!--FlowDocumentScrollViewer VerticalScrollBarVisibility="Disabled">
            <FlowDocument>
                <Table CellSpacing="1" Background="Black" BorderThickness="1" BorderBrush="Black" >
                    <TableRowGroup>
                        <TableRow >
                            <TableCell ColumnSpan="3" BorderThickness="0"  Style="{StaticResource ProductNameStyle}">
                                <Table>
                                    
                                </Table>
                            </TableCell>
                        </TableRow>
                        <TableRow >
                            <TableCell BorderThickness="0"  Style="{StaticResource ExceptionCellStyle}" >
                                <Paragraph>Field 3</Paragraph>
                            </TableCell>
                            <TableCell BorderThickness="0"  Style="{StaticResource ExceptionCellStyle}">
                                <Paragraph>Missing Data</Paragraph>
                            </TableCell>
                            <TableCell BorderThickness="0"  Style="{StaticResource ExceptionCellStyle}">
                                <Paragraph>Enter information</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
            </FlowDocument>
            
        </FlowDocumentScrollViewer-->
    </Grid>

</Window>



HTML Like table Creation in WPF 

http://weblogs.asp.net/kashyapa/archive/2010/01/11/html-like-table-creation-in-wpf.aspx

下载地址:点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值