如何在wpf中的表控件中动态添加新行...?

时间:2016-06-25 07:14:24

标签: c# wpf

FlowDocumentScrollViewer在这个控件中我使用了表和行,但是我想在运行时动态行,当我单击输入Key然后将记录保存到数据库中,并在此表中添加一个新行。

问(1)是否可能......?

如果是,请帮助。

代码在这里:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

                                </TableCell>
                            </TableRow>                               
                            <TableRow>
                                <TableCell>
                                    <Paragraph>F9,F1 P.R.C</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>O.A No</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>O.A SI</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Heat No</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Pour Date</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Grade</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Product</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>W.I.P</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Heat Treat</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Final Clearance</Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>Fett</Paragraph>
                                </TableCell>
                            </TableRow>
                            <TableRow>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="100"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="100"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="50"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="100"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="100"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="100"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="100"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="50"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="30"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="30"></TextBox>
                                    </Paragraph>
                                </TableCell>
                                <TableCell>
                                    <Paragraph>
                                        <TextBox Width="30"></TextBox>
                                    </Paragraph>
                                </TableCell>
                            </TableRow>
                        </TableRowGroup>
                    </Table>
                </FlowDocument>
            </FlowDocumentScrollViewer>

请参阅我的代码图片OutPut

1 个答案:

答案 0 :(得分:2)

我演示了如何使用简单的应用程序向表行添加行。首先,我们需要一些xaml代码:

<Window
    x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <FlowDocumentScrollViewer>
            <FlowDocument>
                <Table Name="tblDummyData">
                    <TableRowGroup>
                        <TableRow>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="1.1" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="1.2" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="1.3" />
                                </Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="2.1" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="2.2" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="2.3" />
                                </Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="3.1" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="3.2" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="3.3" />
                                </Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="4.1" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="4.2" />
                                </Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>
                                    <TextBlock Text="4.3" />
                                </Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
            </FlowDocument>
        </FlowDocumentScrollViewer>

        <Button Content="Add" Click="Button_Click" Grid.Row="1" />
    </Grid>
</Window>

正如你在这里看到的没什么特别的,只是一个包含行和单元格的表以及一个用于添加行的按钮。

所以这就是这段代码背后的原因:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var rowGroup = tblDummyData.RowGroups.FirstOrDefault();

        if (rowGroup != null)
        {
            TableRow row = new TableRow();

            TableCell cell = new TableCell();

            cell.Blocks.Add(new Paragraph(new Run("New Cell 1")));
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Blocks.Add(new Paragraph(new Run("New cell 2")));
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Blocks.Add(new Paragraph(new Run("New cell 3")));
            row.Cells.Add(cell);

            rowGroup.Rows.Add(row);
        }
    }

在这里,您可以看到如何定义一个表Row,而不是根据需要使用尽可能多的单元格填充它,并将其附加到您的行组。在我的情况下,我只有一个行组,但您可以使用linq查询它们并获得您需要的任何行组。

希望有所帮助。