如何在WPF RichTextBox 中以编程方式添加表格?

huangapple go评论46阅读模式
英文:

How can I add tables to a WPF RichTextBox programmatically?

问题

我试图制作一个具有一些常见文字处理功能的RichTextBox,特别是表格。似乎没有直接支持它,但文档似乎指向这样一个观点:你可以用FlowDocuments做的一切都可以用RichTextBox做。

理想情况下,用户将在文本框上方的工具栏上按下按钮,表格将被创建并插入到富文本框中的光标位置。

以下是与文本框和工具栏按钮相关的xaml中的内容:

            <DockPanel>
                <ToolBar Height="30" DockPanel.Dock="Top">
                    <Button Style="{StaticResource formatImageStyle}" Click="InsertTable" ToolTip="Insert Table">
                        <Image Source="/resources/images/table.png"></Image>
                    </Button>
                </ToolBar>
                <RichTextBox x:Name="rtb" SpellCheck.IsEnabled="True" AcceptsTab="True" Margin="10,5,10,0" FontSize="16" MinHeight="350" MaxHeight="350" VerticalScrollBarVisibility="Auto"></RichTextBox>
            </DockPanel>

最后,以下是xaml.cs中到目前为止的内容。我还没有处理光标位置,因为我只是想先尝试将表格放入文本框中。

        private void InsertTable(object sender, RoutedEventArgs e)
        {
            rtb.BeginChange();
            var table = new Table();

            table.Columns.Add(new TableColumn());
            table.Columns.Add(new TableColumn());
            table.Columns.Add(new TableColumn());

            table.RowGroups.Add(new TableRowGroup());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows.Add(new TableRow());

            rtb.Document.Blocks.Add(table);
            rtb.EndChange();
        }

谢谢!
我遵循文档创建了一个表格,然后通过键入将其添加到RichTextBox中:
richTextBoxName.Document.Blocks.Add(tableName);
期望看到表格被添加。但实际上什么也没发生。我确认了代码正在执行。

我还检查了一下,看是否正确设置了代码以编程方式添加内容,我尝试的这段代码片段起作用了:

            rtb.BeginChange();

            TextPointer tp2 = rtb.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
            rtb.CaretPosition.InsertTextInRun("test");
            rtb.CaretPosition = tp2;
            rtb.EndChange();
            Keyboard.Focus(rtb);

我尝试将BeginChange()和EndChange()添加到上面的xaml.cs代码片段中,但也不起作用。

英文:

I'm trying to make a RichTextBox that has some common word processing features, namely tables. It doesn't seem like there is direct support for it, but documentation seems to point to the idea that whatever you can do with FlowDocuments you can do with RichTextBoxes.

Ideally, the user will press a button on a toolbar above the text box, and a table will be created at the caret position in the rich text box.

Here's what I have in the xaml related to the text box and toolbar button so far:

            &lt;DockPanel&gt;
                &lt;ToolBar Height=&quot;30&quot; DockPanel.Dock=&quot;Top&quot;&gt;
                    &lt;Button Style=&quot;{StaticResource formatImageStyle}&quot; Click=&quot;InsertTable&quot; ToolTip=&quot;Insert Table&quot;&gt;
                        &lt;Image Source=&quot;/resources/images/table.png&quot;&gt;&lt;/Image&gt;
                    &lt;/Button&gt;
                &lt;/ToolBar&gt;
                &lt;RichTextBox x:Name=&quot;rtb&quot; SpellCheck.IsEnabled=&quot;True&quot; AcceptsTab=&quot;True&quot; Margin=&quot;10,5,10,0&quot; FontSize=&quot;16&quot; MinHeight=&quot;350&quot; MaxHeight=&quot;350&quot; VerticalScrollBarVisibility=&quot;Auto&quot;&gt;&lt;/RichTextBox&gt;
            &lt;/DockPanel&gt;

Lastly, here's what I have in the xaml.cs so far. I haven't messed with the caret position yet because I just wanted to try to get a table in the box first.

        private void InsertTable(object sender, RoutedEventArgs e)
        {
            rtb.BeginChange();
            var table = new Table();

            table.Columns.Add(new TableColumn());
            table.Columns.Add(new TableColumn());
            table.Columns.Add(new TableColumn());

            table.RowGroups.Add(new TableRowGroup());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows.Add(new TableRow());

            rtb.Document.Blocks.Add(table);
            rtb.EndChange();
        }

Thanks!

I followed documentation to create a table, and then added it to the RichTextBox by typing:
richTextBoxName.Document.Blocks.Add(tableName);

Expecting to see the table get added. Instead, nothing happened. I confirmed the code was being executed.

I also checked to see if I was setting up the code correctly to add things progammatically, and this code snippet I tried worked:

            rtb.BeginChange();

            TextPointer tp2 = rtb.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
            rtb.CaretPosition.InsertTextInRun(&quot;test&quot;);
            rtb.CaretPosition = tp2;
            rtb.EndChange();
            Keyboard.Focus(rtb);

I tried adding BeginChange() and EndChange() to the xaml.cs snippet above, but that didn't work either.

答案1

得分: 1

private void InsertTable(object sender, RoutedEventArgs e)
{
rtb.BeginChange();
var table = new Table();
var gridLengthConverter = new GridLengthConverter();
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());

table.RowGroups.Add(new TableRowGroup());
for (int i = 0; i < 3; i++)
{
    table.RowGroups[0].Rows.Add(new TableRow());
    table.RowGroups[0].Rows[i].Cells.Add(new TableCell());
    table.RowGroups[0].Rows[i].Cells.Add(new TableCell());
    table.RowGroups[0].Rows[i].Cells.Add(new TableCell());
}
rtb.Document.Blocks.Add(table);
rtb.EndChange();

}

This will add an empty table but you can see it if you select the text or press CTRL+A. You can add text to a cell like this

private void InsertTable(object sender, RoutedEventArgs e)
{
rtb.BeginChange();
var table = new Table();
var gridLengthConverter = new GridLengthConverter();
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());

table.RowGroups.Add(new TableRowGroup());
for (int i = 0; i < 3; i++)
{
    table.RowGroups[0].Rows.Add(new TableRow());
    table.RowGroups[0].Rows[i].Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column1"))));
    table.RowGroups[0].Rows[i].Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column2"))));
    table.RowGroups[0].Rows[i].Cells.Add(new TableCell(new Paragraph(new Run("Row" + (i + 1).ToString() + " Column3"))));
}
rtb.Document.Blocks.Add(table);
rtb.EndChange();

}

英文:

You didn't add cells to the rows, try this:

private void InsertTable(object sender, RoutedEventArgs e)
        {
            rtb.BeginChange();
            var table = new Table();
            var gridLenghtConvertor = new GridLengthConverter();
            table.Columns.Add(new TableColumn() );
            table.Columns.Add(new TableColumn() );
            table.Columns.Add(new TableColumn() );

            table.RowGroups.Add(new TableRowGroup());
            for (int i = 0; i &lt; 3; i++)
            {
                table.RowGroups[0].Rows.Add(new TableRow());
                table.RowGroups[0].Rows[i].Cells.Add(new TableCell());
                table.RowGroups[0].Rows[i].Cells.Add(new TableCell());
                table.RowGroups[0].Rows[i].Cells.Add(new TableCell());
            }
            rtb.Document.Blocks.Add(table);
            rtb.EndChange();
        }

This will add an empty table but you can see it if you select the text or press CTRL+A. You can add text to a cell like this

private void InsertTable(object sender, RoutedEventArgs e)
        {
            rtb.BeginChange();
            var table = new Table();
            var gridLenghtConvertor = new GridLengthConverter();
            table.Columns.Add(new TableColumn() );
            table.Columns.Add(new TableColumn() );
            table.Columns.Add(new TableColumn() );

            table.RowGroups.Add(new TableRowGroup());
            for (int i = 0; i &lt; 3; i++)
            {
                table.RowGroups[0].Rows.Add(new TableRow());
                table.RowGroups[0].Rows[i].Cells.Add(new TableCell(new Paragraph(new Run(&quot;Row&quot; + (i + 1).ToString() + &quot; Column1&quot;))));
                table.RowGroups[0].Rows[i].Cells.Add(new TableCell(new Paragraph(new Run(&quot;Row&quot; + (i + 1).ToString() + &quot; Column2&quot;))));
                table.RowGroups[0].Rows[i].Cells.Add(new TableCell(new Paragraph(new Run(&quot;Row&quot; + (i + 1).ToString() + &quot; Column3&quot;))));
            }
            rtb.Document.Blocks.Add(table);
            rtb.EndChange();
        }

huangapple
  • 本文由 发表于 2023年6月5日 04:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定