将表格复制到WPF RichTextBox中失去边界

问题描述

我用FlowDocument生成Table,并在WPF RTF框内替换Selection

但是插入的表对所有单元格使用最后使用的BorderSettings。如果我将生成的XAML加载到FlowDocumentPageViewer内,一切都会很好。

查看器:

enter image description here

RTF错误

enter image description here

请忽略其他格式,只有Border是我的问题,最后一个Border是下划线,但是在RTF中它将覆盖所有其他CellBordersColor在表中。

代码

//GetTable returns the Table
FlowDocument tmp_rtf_tabelle_test =new FlowDocument(((Datenblock_Mitte_Tabelle_Typ5)obj).GetTable());
              
TextRange tr_tabelle = new TextRange(tmp_rtf_tabelle_test.ContentStart,tmp_rtf_tabelle_test.ContentEnd);         

MemoryStream ms_tabelle = new MemoryStream();
                       
//if Dataformats XAML or rtf make no difference
tr_tabelle.Save(ms_tabelle,DataFormats.Xaml);                           
                   
rtf_vorschau.Selection.Load(ms_tabelle,DataFormats.Xaml);
ms_tabelle.Close();

这是生成的XAML:

<Section
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Justify" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="Text" NumberSubstitution.Substitution="AsCulture" FontFamily="Georgia" FontStyle="normal" FontWeight="normal" FontStretch="normal" FontSize="16" Foreground="#FF000000" Typography.Standardligatures="True" Typography.Contextualligatures="True" Typography.discretionaryligatures="False" Typography.Historicalligatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="normal" Typography.Capitals="normal" Typography.NumeralStyle="normal" Typography.NumeralAlignment="normal" Typography.EastAsianWidths="normal" Typography.EastAsianLanguage="normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0">
    <Table Padding="0,0" BorderThickness="1,1,1">
        <Table.Columns>
            <TableColumn Width="300" />
            <TableColumn Width="419" />
        </Table.Columns>
        <TableRowGroup>
            <TableRow>
                <TableCell ColumnSpan="2" BorderThickness="1,1" BorderBrush="#FFFF0000">
                    <Paragraph Background="#FFD3D3D3">
                        <Run> 5. Weitere Vorgehensweise</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell Padding="0,0" BorderBrush="#FF0000FF">
                    <Paragraph LineHeight="1" Margin="0,0">
                        <Run> Haftung:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell BorderThickness="0,1" BorderBrush="#FFFFFF00">
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Deckung:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Plausibilität:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Zahlung in €:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>0,00</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Reserve in €:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>0,00</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell>
                    <Paragraph>
                        <Run> Unterlagen folgen:</Run>
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <Run>&lt;Bitte wählen&gt;</Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
            <TableRow>
                <TableCell ColumnSpan="2">
                    <Paragraph Margin="3,3,3">
                        <Run></Run>
                    </Paragraph>
                </TableCell>
            </TableRow>
        </TableRowGroup>
    </Table>
</Section>

更新:我用Tables创建了一个RTF文件并加载了它们(FileStream),但是结果是相同的。 BorderSettings丢失了。

解决方法

根据加载包含带有彩色边框的表格的RTF文档时的问题描述,边框会失去颜色并显示为黑色。

假设原始.rtf文档包括以下内容 带有黄色单元格边框的表格:

enter image description here

将此文档加载到RichTextBox控件表之后 如下所示:

enter image description here

不幸的是,这是在WPF .rtf控件中显示使用Microsft Word准备的RichTextBox个文档的已知限制。

MSDN社区支持人员写道:

我加载rtf文档并显示在WPF RitchTextBox中,它丢失了 格式的一部分(使用Word打开Normal)。如果要打开它 对于WPF RitchTextBox,我建议使用WPF RitchTextBox保存 RTF文件打开以确保格式一致。您可以加载,编辑和 在WPF中保存RTF格式。工具可能会丢失某些样式 转换。

有关更多信息,请参见Microsoft网站上的文章:rtf document displayed in RitchTextBox display wrong colors