我可以在wpf中保存textrange inline属性到rtf吗?

问题描述 投票:0回答:1
因此,我正在写一个模仿单词的项目。 设置相对简单:

  • 我从数据库中获取nvarchar(最大)。 (这是RTF格式)。

    加载RichTextbox时,请转换RTF。

    using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString))) { TargetRichTextBox.Selection.Load(stream, DataFormats.Rtf); }
  • 我做我的事情并保存回数据库。
    public string ConvertRichTextBoxToRtf(RichTextBox richTextBox)
                {
                    if (richTextBox == null) throw new ArgumentNullException(nameof(richTextBox));
    
                    using (var memoryStream = new System.IO.MemoryStream())
                    {
                        var range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                        range.Save(memoryStream, DataFormats.Rtf);
                        return System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                    }
                }
    
    
  • 问题:
    
    
  • 我使用TexTrange.ApplyPropertyValue(inline.propname,value)设置的所有属性 运行时工作。但是似乎没有保存到RTF字符串。 而所有TexTrange.ApplyPropertyValue(textElement.Propname,value)属性的行为如预期的。
我应该更改为XML或XAML以存储我的字符串吗? 我的记忆流是错误的格式吗? 还是我使用的是错误的属性? the是“内联”属性代码的一个示例:

public void SetSelectionToSubscript(TextRange range) { if (range.IsEmpty) return; // Don't apply formatting to an empty selection object currentBaseline = range.GetPropertyValue(Inline.BaselineAlignmentProperty); if (currentBaseline is BaselineAlignment alignment && alignment == BaselineAlignment.Subscript) { // Reset to normal text range.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Baseline); range.ApplyPropertyValue(TextElement.FontSizeProperty, MyFondSize); // Reset font size } else { // Apply subscript formatting range.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript); range.ApplyPropertyValue(TextElement.FontSizeProperty, MyFondSize - 2); // Adjust font size for subscript effect } }

提前感谢

您的代码正确。不幸的是,WPF

RichTextBox

控制不支持

RTF格式规范中描述的许多格式。

在进行解决方案时,您可以在保存/加载文档时使用
DataFormats.Xaml

c# wpf rtf textrange
1个回答
0
投票
DataFormats.XamlPackage

。
其他解决方案正在使用一些第三方库,例如wpfrichtextbox

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.