我看到了如何设置 WPF RichTextBox Class 中的富文本框.
然而我喜欢像以前一样将它的文本保存到数据库中,在 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;
}
关于wpf - 如何将 WPF 富文本框转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4125310/