How to get a WPF rich textbox into a string

与世无争的帅哥 提交于 2019-12-19 15:34:13

问题


I saw how to set a WPF rich text box in RichTextBox Class.

Yet I like to save its text to the database like I used to, in Windows Forms.

string myData = richTextBox.Text;
dbSave(myData);

How can I do it?


回答1:


At the bottom of the MSDN RichTextBox reference there's a link to How to Extract the Text Content from a 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;
}


来源:https://stackoverflow.com/questions/4125310/how-to-get-a-wpf-rich-textbox-into-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!