我看到了如何设置WPF富文本框RichTextBox Class。如何将WPF富文本框变为字符串
然而,我喜欢将它的文本保存到数据库中,就像我曾经在Windows Forms中那样。
string myData = richTextBox.Text;
dbSave(myData);
我该怎么办?
我看到了如何设置WPF富文本框RichTextBox Class。如何将WPF富文本框变为字符串
然而,我喜欢将它的文本保存到数据库中,就像我曾经在Windows Forms中那样。
string myData = richTextBox.Text;
dbSave(myData);
我该怎么办?
在MSDN RichTextBox参考的底部,有对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;
}
+1:这对于如此基本的事情来说有点复杂。在大多数时间不需要控制开始和结束是很有用的,我仍然期望.text或.context等。 – Asaf 2010-11-08 15:55:29
@Asaf我不认为这很复杂,RichTextBox不是纯文本文档。有与RichTextBox相关联的格式,样式等,因此具有基于对象的支持是有意义的。 – 2010-11-08 15:59:13
你可能是对的,但我在这里放松我的头发安静快速:像设置文本,clearText(=“”),或将字符串值放在函数中的基础知识都避免了我。它可能是有道理的,但它根本不友好。 – Asaf 2010-11-08 16:10:41
为什么要用一个RichTextBox,如果你只想要的文字?如果只是使用TextBox而不是更好? :) – Arcturus 2010-11-08 15:51:20