如何将 WPF 富文本框转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4125310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a WPF rich textbox into a string
提问by Asaf
I saw how to set a WPFrich text box in RichTextBox Class.
我看到了如何在RichTextBox Class中设置WPF富文本框。
Yet I like to save its text to the database like I used to, in Windows Forms.
然而,我喜欢像以前一样在Windows Forms中将其文本保存到数据库中。
string myData = richTextBox.Text;
dbSave(myData);
How can I do it?
我该怎么做?
回答by Gavin Miller
At the bottom of the MSDN RichTextBoxreference there's a link to How to Extract the Text Content from a RichTextBox
在 MSDN RichTextBox参考的底部,有一个指向如何从 RichTextBox 中提取文本内容的链接
It's going to look like this:
它看起来像这样:
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;
}