[英]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.