RichTextBox(WPF)没有名为“Text”的字符串属性。

126

我正在尝试设置/获取RichTextBox的文本,但是当我想要获取test.Text时,Text不在其属性列表中...

我正在使用C#(.net framework 3.5 SP1)的代码后端。

RichTextBox test = new RichTextBox();

无法使用 test.Text(?)

你知道可能的原因吗?

11个回答

139

设置 RichTextBox 的文本:

richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));

获取 RichTextBox 文本的方法:

string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;

2
构造函数'Run'没有参数,但是被传入了1个参数,同样适用于'Paragraph'。 - alvinmeimoun
@alvinmeimoun 实际上,Paragraph() 有一个 Paragraph(Inline) 的重载至少从 .NET 3.5 开始(而且 Run(string) 也是有效的 - 它甚至在示例中)。 - Dragomok
4
为什么这么复杂? - user3763113
1
如何在段落中添加“FontFamily”? - Matheus Miranda

67

System.Windows.FormsSystem.Windows.Control 中,RichTextBox 之间存在混淆。

由于我正在使用 WPF,因此我使用的是 Control 中的 RichTextBox。在那里,没有 Text 属性,为了获取文本,我应该使用这行代码:

string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text; 

谢谢


39

WPF 的 RichTextBox 有一个名为 Document 的属性,用于设置内容,类似于 MSDN 上的描述:

// Create a FlowDocument to contain content for the RichTextBox.
        FlowDocument myFlowDoc = new FlowDocument();

        // Add paragraphs to the FlowDocument.
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
        myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
        RichTextBox myRichTextBox = new RichTextBox();

        // Add initial content to the RichTextBox.
        myRichTextBox.Document = myFlowDoc;
你只需要使用AppendText方法就可以实现你想要的功能。
希望能对你有所帮助。

18
使用两个扩展方法,这变得非常容易:
public static class Ext
{
    public static void SetText(this RichTextBox richTextBox, string text)
    {
        richTextBox.Document.Blocks.Clear();
        richTextBox.Document.Blocks.Add(new Paragraph(new Run(text)));
    }

    public static string GetText(this RichTextBox richTextBox)
    {
        return new TextRange(richTextBox.Document.ContentStart,
            richTextBox.Document.ContentEnd).Text;
    }
}

16

WPF RichTextBox控件中没有Text属性。以下是一种获取所有文本的方法:

TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);

string allText = range.Text;

13
string GetString(RichTextBox rtb)
{
    var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    return textRange.Text;
}

10
RichTextBox rtf = new RichTextBox();
System.IO.MemoryStream stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(yourText));

rtf.Selection.Load(stream, DataFormats.Rtf);

rtf.Selection.Text = yourText;

8
那我们只需执行以下操作:
_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;

1
到目前为止,这是我能找到的最佳答案 :) 在GUI中,如果您想将长度粘贴到另一个文本框中,这是我的代码: rtxb_input.SelectAll(); txb_InputLength.Text = rtxb_input.Selection.Text.Length.ToString(); - Marty_in_a_Box

4
"Extended WPF Toolkit"现在提供了一个带有文本属性的richtextbox。
您可以以不同的格式(XAML、RTF和纯文本)获取或设置文本。
这里是链接:扩展WPF工具包RichTextBox

0
令我大为惊讶的是,RichtTextBox 返回的值与设置的值不同!使用以下代码设置字符串:
SelectAll()
RichTextBox.Selection.Text = "AA"

并返回:

SelectAll()
Return RichTextBox.Selection.Text

返回带回车的“AA”

同时使用:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Selection.Text = "AA"

并返回:

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd)
Return Selection.Text

使用相同的“AA”和回车符是否一样?

RichTextBox不会返回设置的值,这是非常不正确的行为!!

可以通过以下方式解决(规避):

Dim Selection = New TextRange(rtbRichTextBox.Document.ContentStart, rtbRichTextBox.Document.ContentEnd.GetPositionAtOffset(-1))
Return Selection.Text

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接