如何将 WPF 富文本框转换为字符串 [英] How to get a WPF rich textbox into a string
本文介绍了如何将 WPF 富文本框转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到了如何在 WPF 富文本框"https://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx" rel="noreferrer">RichTextBox 类.
I saw how to set a WPF rich text box in RichTextBox Class.
但我喜欢像以前一样将其文本保存到数据库中,在 Windows 窗体.
Yet I like to save its text to the database like I used to, in Windows Forms.
string myData = richTextBox.Text;
dbSave(myData);
我该怎么做?
推荐答案
在 MSDN 底部 RichTextBox 参考有一个链接到 如何从 RichTextBox 中提取文本内容
At the bottom of the MSDN RichTextBox reference there's a link to How to Extract the Text Content from a RichTextBox
它看起来像这样:
public string RichTextBoxExample()
{
RichTextBox myRichTextBox = new RichTextBox();
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;
// Let's pretend the RichTextBox gets content magically ...
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
myRichTextBox.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
myRichTextBox.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}
这篇关于如何将 WPF 富文本框转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文