[英]RichTextBox (WPF) does not have string property “Text”
我正在嘗試設置/獲取我的RichTextBox的文本,但當我想獲得test.Text時,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。 在那里,沒有Text屬性,為了獲得文本,我應該使用這一行:
string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text;
謝謝
在WPF的RichTextBox有一個Document
用於設置內容的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
方法,如果這就是你所追求的全部。
希望有所幫助。
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
使用兩種擴展方法,這變得非常簡單:
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;
}
}
WPF RichTextBox控件中沒有Text
屬性。 這是獲取所有文本的一種方法:
TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);
string allText = range.Text;
如何做到以下幾點:
_richTextBox.SelectAll();
string myText = _richTextBox.Selection.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;
“Extended WPF Toolkit”現在提供帶有Text屬性的richtextbox。
您可以以不同的格式(XAML,RTF和純文本)獲取或設置文本。
這是鏈接: 擴展WPF工具包RichTextBox
在我的情況下,我不得不將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;
}
根據它,它確實有一個Text屬性
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx
如果您希望將文本拆分為線條,也可以嘗試“線條”屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.