为什么我的 WPF RichTextBox 在保存并再次加载后不保留上标和下标数据?
Why doesn't my WPF RichTextBox keep the Superscript and Subscript data after saving and loading it again?
我是新来的所以如果我没有正确地问这个问题我提前道歉。
目前,我正在 WPF、C# 和 .Net 4.5[ 中开发一个小型 TextEditor 项目=35=],但我最近发现了一个我似乎无法克服的问题。出于某种原因,转换为 Rtf 以保存建议的 RichTextBox 不会保存 BaselineAlignmentProperty
(上标和下标),这应该是可能的(根据 Rtf 1.5 规范:http://www.biblioscape.com/rtf15_spec.htm 下标和上标支持 (\sub ; \super & \nosupersub)).
我用来将 RichTextBox 转换为 Rtf 并将 Rtf 加载到 RichTextBox 的代码 (Storing data of rich text box to database with formatting):
public static string ToRtf(RichTextBox RichText)
{
string RtfText;
TextRange Text = new TextRange(RichText.Document.ContentStart, RichText.Document.ContentEnd);
using (MemoryStream MS = new MemoryStream())
{
Text.Save(MS, DataFormats.Rtf);
RtfText = Encoding.ASCII.GetString(MS.ToArray());
}
return RtfText;
}
public static void LoadRtfData(RichTextBox TextBox, string RtfText)
{
byte[] Data = Encoding.ASCII.GetBytes(RtfText);
using (MemoryStream MS = new MemoryStream(Data))
{
TextRange Text = new TextRange(TextBox.Document.ContentStart, TextBox.Document.ContentEnd);
Text.Load(MS, DataFormats.Rtf);
}
}
此外,将 DataFormat 更改为 Xaml
或 XamlPackage
(如此处描述的可能性:http://umaranis.com/2010/11/29/save-and-load-richtextbox-content-in-wpf/)会导致崩溃。
我用来更改 MyTextBox
处所选文本的 BaselineAligmentProperty
的代码:
MyTextBox.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Bottom/*BaselineAligment.Subscript doesn't work either*/);
当 saving/loading 时,我应该如何使我的 RichTextBox Rtf 包含 BaselineAlignment 数据?
设法通过将 DataFormat 更改为 Xaml 使其工作。
我是新来的所以如果我没有正确地问这个问题我提前道歉。
目前,我正在 WPF、C# 和 .Net 4.5[ 中开发一个小型 TextEditor 项目=35=],但我最近发现了一个我似乎无法克服的问题。出于某种原因,转换为 Rtf 以保存建议的 RichTextBox 不会保存 BaselineAlignmentProperty
(上标和下标),这应该是可能的(根据 Rtf 1.5 规范:http://www.biblioscape.com/rtf15_spec.htm 下标和上标支持 (\sub ; \super & \nosupersub)).
我用来将 RichTextBox 转换为 Rtf 并将 Rtf 加载到 RichTextBox 的代码 (Storing data of rich text box to database with formatting):
public static string ToRtf(RichTextBox RichText)
{
string RtfText;
TextRange Text = new TextRange(RichText.Document.ContentStart, RichText.Document.ContentEnd);
using (MemoryStream MS = new MemoryStream())
{
Text.Save(MS, DataFormats.Rtf);
RtfText = Encoding.ASCII.GetString(MS.ToArray());
}
return RtfText;
}
public static void LoadRtfData(RichTextBox TextBox, string RtfText)
{
byte[] Data = Encoding.ASCII.GetBytes(RtfText);
using (MemoryStream MS = new MemoryStream(Data))
{
TextRange Text = new TextRange(TextBox.Document.ContentStart, TextBox.Document.ContentEnd);
Text.Load(MS, DataFormats.Rtf);
}
}
此外,将 DataFormat 更改为 Xaml
或 XamlPackage
(如此处描述的可能性:http://umaranis.com/2010/11/29/save-and-load-richtextbox-content-in-wpf/)会导致崩溃。
我用来更改 MyTextBox
处所选文本的 BaselineAligmentProperty
的代码:
MyTextBox.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Bottom/*BaselineAligment.Subscript doesn't work either*/);
当 saving/loading 时,我应该如何使我的 RichTextBox Rtf 包含 BaselineAlignment 数据?
设法通过将 DataFormat 更改为 Xaml 使其工作。