c# - 在 RTF 字符串中设置格式?

标签 c# wpf mvvm richtextbox rtf

我在 View 模型中使用“粘贴”按钮命令从剪贴板复制 RTF。 PastedText是我认为 RichTextBox 绑定(bind)到的字符串属性:

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

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

我只会使用 PastedText += Clipboard.GetText();
获取纯文本,但它以不同的字体大小粘贴,我需要 RTF 格式。我已经研究过迭代 RTF 字符串并对字体大小、颜色等进行查找/替换,但即使对于几个单词,RTF 也非常复杂。

有没有办法解决?谢谢

最佳答案

最后,我使用 View 中的代码来使用“格式”按钮从 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 逻辑。

关于c# - 在 RTF 字符串中设置格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22913437/

相关文章:

wpf - 谁将 listviewcolumn 标题绑定(bind)到 WPF 中的列单元格

c# - 在捕获输入记录时,如何正确使用 ViewModel 构建与 View 的双向绑定(bind)?

c# - 无法更改DataGrid中的前景

c# - 无法进入 .NET Framework 源代码

c# - C# AnyCPU 项目能否包含平台特定的 dll

c# - 如何向 OxyPlot 添加新点?

asp.net-mvc - 在 Kendo UI ASP.net MVC 中使用 ViewModel

c# - 如何以编程方式滚动 TreeView 控件?

c# - 我可以更改 Environment.NewLine 的值吗?

wpf - WPF 中的 Web 浏览器控件是否足够完美以支持现代浏览器功能?