如何将 BlockUIContainer 中 RichTextBox 的内容保存为 RTF 文件? [英] How to save content of RichTextBox in BlockUIContainer, to RTF file?
问题描述
我使用 BlockUIContainer 制作了两个richtexbox,一个小的richtexbox 嵌套在一个大的richtextbox 中,请参阅下面的XAML 文件.
I made two richtexbox, one small richtexbox nested in a big richtextbox by using BlockUIContainer, see XAML file below.
<RichTextBox IsDocumentEnabled="True" x:Name="first" HorizontalAlignment="Left" Height="174" Margin="120,136,0,0" VerticalAlignment="Top" Width="254" TextChanged="first_TextChanged" MouseDoubleClick="first_MouseDoubleClick">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox"/>
</Paragraph>
<BlockUIContainer>
<RichTextBox IsReadOnly="True" x:Name="second" MouseDoubleClick="second_MouseDoubleClick" Width="239" TextChanged="second_TextChanged">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox1"/>
<Run Language="en" Text="hh"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</BlockUIContainer>
</FlowDocument>
</RichTextBox>
我想将两个 Richtexbox 的内容保存在一个 RTF 文件中.通常我们可以使用 TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd),因为现在我们已经嵌套了 Richtexbox,所以我认为 TextRange 可能无法正常工作.也许我会在richtextbox中添加一些图像,所以我想尝试二进制流,见下文.第一个"是大富文本框的名称.THE PATH 是 RTF 文件的路径.
I want contents of both richtexbox saved in a RTF file. Normally we can use TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd), since now we have nested richtexbox, so i think TextRange is probablly not working. And maybe i will add some images in the richtextbox, so I want to try binary stream, see below. "first" is the name of big richtextbox. THE PATH is the path for RTF file.
FlowDocument document = first.Document;
System.IO.Stream ms = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(document, ms);
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, data.Length);
ms.Close();
File.WriteAllBytes(THE PATH, data);
但是在保存的 RTF 文件中,当我打开它时,它显示如下,
But in the saved RTF file, when i open it, it shows like this,
<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Paragraph>RichTextBox</Paragraph><Paragraph><Run xml:lang="en">test test test!!!</Run></Paragraph><BlockUIContainer><RichTextBox IsReadOnly="True" Name="second" Width="239"><FlowDocument PagePadding="5,0,5,0" AllowDrop="True"><Paragraph>RichTextBox1<Run xml:space="preserve"> </Run><Run xml:lang="en">hhthis is a small richtextbox</Run></Paragraph><Paragraph><Run xml:lang="en" xml:space="preserve" /></Paragraph></FlowDocument></RichTextBox></BlockUIContainer></FlowDocument>
我不想在保存的文件中看到 XAML 纯文本,我希望它像普通的 Word 文档一样显示,见下文.大概还可以看到小的richtextbox的边框线(这条边框线下面没有显示)
I don´t want to see XAML plain text in the saved file, I want it to show like a normal word document, see below. and probably also see the border line of the small richtextbox (this border line doesnot show below)
RichTextBox
test test test!!!
RichTextBox1 hhthis is a small richtextbox
我该怎么办?或者 BlockUIContainer 嵌套的 Richtextbox 不能这样显示?
What should I do? or nested richtextbox by BlockUIContainer is not possible to show like this?
推荐答案
您可以通过保存并指定格式来实现.我省略了不必要的代码,只是将 memorystream 替换为您当前拥有的代码.
You can do it by saving it and specifying the format. I omit the unnecessary code, but just replace memorystream with what you currently have.
richTextBox.SelectAll();
richTextBox.Selection.Save(new MemoryStream(), DataFormats.Rtf);
XamlWriter
将其保存为 XAML 格式
XamlWriter
will save it as a XAML format
这篇关于如何将 BlockUIContainer 中 RichTextBox 的内容保存为 RTF 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!