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

下载地址:点击打开链接


WPF 中,有多种避免报错的代码实现方式: ### 设计状态下避免报错 在 WPF XML 文件被打开时,设计状态下 VS 需要渲染界面到设计器中,需要执行对应类中的构造方法和 Load 方法,此时如果这些方法报错会导致设计界面加载失败。可以通过添加判断代码判断是否处于设计模式,如果是则返回。示例如下: ```csharp if (DesignerProperties.GetIsInDesignMode(this)) { return; } ``` 这样可以避免在设计模式下执行可能会报错的代码,保证设计界面能正常加载[^1]。 ### 避免空引用异常 对于可能出现空引用的情况,需要进行空值检查。例如在处理数组或对象时,先判断其是否为 null 再进行操作。如下面的代码在添加到列表框之前检查数组元素是否为 null 或空字符串: ```csharp string[] informTable = new string[50]; Service1 ser2 = new Service1(); informTable = ser2.ShowDatabaseBtnSearch2(mailbox1); for (int i = 0; i < informTable.Length; i++) { if (informTable[i] != null && informTable[i] != "") { listbox1.Items.Add(informTable[i]); } } ``` 通过这种空值检查可以避免 `System.NullReferenceException` 异常[^2]。 ### 数据绑定序列化报错处理 在数据继承并实现了 `INotifyPropertyChanged` 接口情况下,数据绑定到界面后,再对数据进行序列化,会报错。可以使用 `[field:NonSerialized]` 修饰 `PropertyChanged` 事件,示例如下: ```csharp [field:NonSerialized] public event PropertyChangedEventHandler PropertyChanged; ``` 这样可以避免在序列化时出现问题[^3]。 ### 重写窗口关闭事件避免报错 在重写 `OnClosing` 窗口关闭事件时,要注意避免调用可能导致报错的代码。例如避免在重写方法中直接调用 `this.Close()` 导致的报错情况,正确的做法是使用 `e.Cancel` 来控制窗口是否关闭: ```csharp protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { if (System.Windows.Forms.MessageBox.Show("确定是否关闭当前应用程序?", "提示", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { e.Cancel = false; } else { e.Cancel = true; this.Hide(); } } ``` 这样可以正确处理窗口关闭事件,避免报错[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值