wpf - 如何将 WPF 富文本框转换为字符串

标签 wpf richtextbox

我看到了如何设置 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/

相关文章:

ckeditor - 集成 Markdown 所见即所得文本编辑器

vb.net - RichTextBox - 删除最后一个字符而不删除格式

c# - 查找控件的直接祖先/父级

wpf - 如何以编程方式在 WPF ListView 中设置选择标记(焦点项目)

c# - 单击按钮以获取数据wpf mvvm

vb6 - VB6 RichTextBox 中 creatin 文本部分的格式化颜色

c# Winform richtextbox字体差异

c# - Wpf Web 浏览器刷新

wpf - 如何更改按钮中文本 block 的样式

c# - WPF RichTextBox - ScrollToEnd() 方法不会导致发生任何滚动