在RichTextBox内的表中添加和删除行 [英] Add and Remove row from a Table inside of RichTextBox

查看:142
本文介绍了在RichTextBox内的表中添加和删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

当我在RichTextBox中创建并粘贴表格时。我突然需要在RichTextBox中的表中删除或添加一行。我该怎么办?





XAML



Problem:
When I have created and pasted the table inside of RichTextBox. Suddently I need to remove or add a row in the table that is located in RichTextBox. How should I do it?


XAML

Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <RichTextBox x:Name="newRtb" />
    </Grid>
</Window>









C#







C#

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   var tab = new Table();
   var gridLenghtConvertor = new GridLengthConverter();

   tab.Columns.Add(new TableColumn() { Name = "Column1", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
   tab.Columns.Add(new TableColumn() { Name = "Column2", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });

   tab.RowGroups.Add(new TableRowGroup());

   for(int i=0;i<10;i++)
   {
      tab.RowGroups[0].Rows.Add(new TableRow());
      var tabRow = tab.RowGroups[0].Rows[i];

      tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column1"))) { TextAlignment = TextAlignment.Center });
      tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column2"))));
   }
   
   newRtb.Document.Blocks.Add(tab);
}

推荐答案

<pre>private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var tab = new Table();
            tab.Name = "MyTable";
            newRtb.RegisterName("MyTable", tab);


            var gridLenghtConvertor = new GridLengthConverter();

            tab.Columns.Add(new TableColumn() { Name = "Column1", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });
            tab.Columns.Add(new TableColumn() { Name = "Column2", Width = (GridLength)gridLenghtConvertor.ConvertFromString("*") });

            tab.RowGroups.Add(new TableRowGroup());

            for (int i = 0; i < 10; i++)
            {
                tab.RowGroups[0].Rows.Add(new TableRow());
                var tabRow = tab.RowGroups[0].Rows[i];

                tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column1"))) { TextAlignment = TextAlignment.Center });
                tabRow.Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column2"))));
            }


            newRtb.Document.Blocks.Add(tab);

        }






Table tab  = newRtb.FindName("MyTable") as Table;

            //add row:
            tab.RowGroups[0].Rows.Add(new TableRow());
            var tabRow = tab.RowGroups[0].Rows[tab.RowGroups[0].Rows.Count - 1];

            tabRow.Cells.Add(new TableCell(new Paragraph(new Run("NEW ROW C1"))) { TextAlignment = TextAlignment.Center });
            tabRow.Cells.Add(new TableCell(new Paragraph(new Run("NEW ROW C2"))));

            //remove first row:
            int index = 0;
            tab.RowGroups[0].Rows.RemoveAt(index);


这篇关于在RichTextBox内的表中添加和删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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