RichTextBox(WPF)没有string属性“文本”
我想设置/获取我的RichTextBox的文本,但文本不在其属性的列表中,当我想获得test.Text …
我在C#中使用代码(.net framework 3.5 SP1)
RichTextBox test = new RichTextBox();  不能有test.Text(?) 
你知道怎么可能呢?
设置 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; 
RichTextBox在System.Windows.Forms和System.Windows.Control之间有混淆
我正在使用WPF中的控件中的一个。 在那里,没有文本属性,为了得到一个文本,我应该使用这一行:
 string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text; 
谢谢
  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; 
 尽pipe如此,你可以使用AppendText方法。 
希望有所帮助。
 string GetString(RichTextBox rtb) { var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd); return textRange.Text; } 
 在WPF RichTextBox控件中没有Text属性。 这里有一个方法可以把所有的文字都拿出来: 
 TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd); string allText = range.Text; 
 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; 
使用两种扩展方法,这变得非常简单:
 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; } } 
如何做如下:
 _richTextBox.SelectAll(); string myText = _richTextBox.Selection.Text; 
“扩展WPF工具包”现在提供了一个带有Text属性的richtextbox。
您可以获取或设置不同格式的文本(XAML,RTF和明文)。
这里是链接: 扩展WPF工具包RichTextBox
据此它有一个文本属性
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx
如果要将文本分解为行,也可以尝试“行”属性。