首页 文章

在RTF字符串中设置格式?

提问于
浏览
1

我'm using a '在我的视图中粘贴'按钮命令模型从剪贴板复制RTF . PastedText 是我的视图中RichTextBox绑定的字符串属性:

private void FormatPastedTextCommandAction()
 {
    PastedText += Clipboard.GetText(TextDataFormat.Rtf);                   
 }

这样可以在按下“粘贴”按钮时粘贴文本 . 但是,我想锁定粘贴功能的格式并从粘贴的RTF字符串中删除所有格式(颜色,斜体,设置为黑色Arial 12) .

我会用 PastedText += Clipboard.GetText();

获取纯文本,但它以不同的字体大小粘贴,我需要RTF格式 . 我已经看过迭代RTF字符串并对字体大小,颜色等进行查找/替换,但即使是几个单词,RTF也非常复杂 .

有没有办法解决?谢谢

1 回答

  • 0

    最后,我在视图中使用了代码,使用“格式”按钮从RichTextBox本身剥离格式:

    private void _btnFormat_Click(object sender, RoutedEventArgs e)
        {
            TextRange rangeOfText = new TextRange(richTextBoxArticleBody.Document.ContentStart, richTextBoxArticleBody.Document.ContentEnd);
            rangeOfText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
            rangeOfText.ApplyPropertyValue(TextElement.FontSizeProperty, "12");
            rangeOfText.ApplyPropertyValue(TextElement.FontFamilyProperty, "Arial");
            rangeOfText.ApplyPropertyValue(TextElement.FontStyleProperty, "Normal");
            rangeOfText.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
            rangeOfText.ApplyPropertyValue(Paragraph.MarginProperty, new Thickness(0));
    
        }
    

    这样做很好,并没有真正打破MVVM模式,因为代码只是UI逻辑 .

相关问题