我在 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/