【问题标题】:RichTextBox (WPF) does not have string property "Text"RichTextBox (WPF) 没有字符串属性“Text”
【发布时间】:2010-10-31 17:49:17
【问题描述】:

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

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

RichTextBox test = new RichTextBox();

不能有test.Text(?)

你知道怎么可能吗?

【问题讨论】:

    标签: c# wpf wpf-controls richtextbox


    【解决方案1】:

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

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

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

    【讨论】:

    • 这是 WPF,不是 Win Forms。
    【解决方案2】:

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

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

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

    谢谢

    【讨论】:

      【解决方案3】:

      WPF RichTextBox 有一个Document 属性用于设置内容a la 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 方法,但如果这就是您所追求的。

      希望对您有所帮助。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:
          string GetString(RichTextBox rtb)
          {
              var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
              return textRange.Text;
          }
          

          【讨论】:

            【解决方案6】:

            “扩展 WPF 工具包”现在提供了一个带有 Text 属性的富文本框。

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

            这里是链接:Extended WPF Toolkit RichTextBox

            【讨论】:

              【解决方案7】:

              只需执行以下操作如何:

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

              【讨论】:

              • 迄今为止我能找到的最佳答案 :) 如果您想将长度粘贴到 GUI 中的另一个文本框中,我的代码如下:rtxb_input.SelectAll();txb_InputLength.Text = rtxb_input.Selection.Text.Length.ToString();
              【解决方案8】:

              设置 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;
              

              【讨论】:

              • Contructor 'Run' 有 0 个参数,但使用 1 个参数调用,段落相同
              • @alvinmeimoun 实际上,Paragraph() 有一个 Paragraph(Inline) 重载 at least since .NET 3.5(并且 Run(string) 也是有效的 - 甚至在示例中也是如此)。
              • 为什么这么复杂?
              • 如何在段落中添加FontFamily
              【解决方案9】:
              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;
              

              【讨论】:

                【解决方案10】:

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

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-04-06
                  • 1970-01-01
                  相关资源
                  最近更新 更多