首页 文章

RichTextBox(WPF)没有字符串属性“Text”

提问于
浏览
107

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

我在C#中使用代码(.net framework 3.5 SP1)

RichTextBox test = new RichTextBox();

不能 test.Text(?)

你知道怎么可能吗?

11 回答

  • 6

    set RichTextBox文本:

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

    to get RichTextBox文本:

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

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

    我正在使用控件中的那个,因为我正在使用WPF . 在那里,没有Text属性,为了获得文本,我应该使用这一行:

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

    谢谢

  • 8

    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 方法,如果's all you'之后 .

    希望有所帮助 .

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

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

    TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);
    
    string allText = range.Text;
    
  • 12
    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;
    
  • -9

    使用两种扩展方法,这变得非常简单:

    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;
        }
    }
    
  • 63

    如何做到以下几点:

    _richTextBox.SelectAll();
    string myText = _richTextBox.Selection.Text;
    
  • 38

    “Extended WPF Toolkit”现在提供带有Text属性的richtextbox .

    您可以以不同的格式(XAML,RTF和纯文本)获取或设置文本 .

    这是链接:Extended WPF Toolkit RichTextBox

  • 10

    在我的情况下,我不得不将RTF文本转换为纯文本:我在GiangLP答案中所做的(使用Xceed WPF Toolkit);并在扩展方法中设置我的代码:

    public static string RTFToPlainText(this string s)
        {
           // for information : default Xceed.Wpf.Toolkit.RichTextBox formatter is RtfFormatter 
            Xceed.Wpf.Toolkit.RichTextBox rtBox = new Xceed.Wpf.Toolkit.RichTextBox(new System.Windows.Documents.FlowDocument());
            rtBox.Text = s;
            rtBox.TextFormatter = new Xceed.Wpf.Toolkit.PlainTextFormatter();
            return rtBox.Text;
    
        }
    
  • 111

    根据它,它确实有一个Text属性

    http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx

    如果您希望将文本拆分为线条,也可以尝试“线条”属性 .

相关问题