C# RichTextBox (WPF) 没有字符串属性“Text”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/957441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RichTextBox (WPF) does not have string property "Text"
提问by
I am trying to set/get the text of my RichTextBox, but Text is not among list of its properties when I want to get test.Text...
我正在尝试设置/获取 RichTextBox 的文本,但是当我想要获取 test.Text 时,Text 不在其属性列表中...
I am using code behind in C# (.net framework 3.5 SP1)
我在 C# (.net framework 3.5 SP1) 中使用代码
RichTextBox test = new RichTextBox();
cannot have test.Text(?)
不能有 test.Text(?)
Do you know how come it can be possible ?
你知道怎么可能吗?
回答by Jaime Garcia
According to this it does have a Text property
据此,它确实有一个 Text 属性
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox_members.aspx
You can also try the "Lines" property if you want the text broken up as lines.
如果您希望将文本分解为行,您也可以尝试“行”属性。
回答by Jaime Garcia
There was a confusion between RichTextBox in System.Windows.Formsand in System.Windows.Control
System.Windows.Forms和System.Windows.Control 中的RichTextBox 之间存在混淆
I am using the one in the Control as I am using WPF. In there, there is no Text property, and in order to get a text, I should have used this line:
我正在使用控件中的一个,因为我正在使用 WPF。在那里,没有 Text 属性,为了获得文本,我应该使用这一行:
string myText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text;
thanks
谢谢
回答by EightyOne Unite
The WPF RichTextBox has a Document
property for setting the content a laMSDN:
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;
You can just use the AppendText
method though if that's all you're after.
AppendText
如果这就是您所追求的,您可以只使用该方法。
Hope that helps.
希望有帮助。
回答by Chris Amelinckx
There is no Text
property in the WPF RichTextBox control. Here is one way to get all of the text out:
Text
WPF RichTextBox 控件中没有属性。这是获取所有文本的一种方法:
TextRange range = new TextRange(myRTB.Document.ContentStart, myRTB.Document.ContentEnd);
string allText = range.Text;
回答by user1143839
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
回答by GiangLP
"Extended WPF Toolkit" now provides a richtextbox with the Text property.
“扩展 WPF 工具包”现在提供带有 Text 属性的富文本框。
You can get or set the text in different formats (XAML, RTF and plaintext).
您可以获取或设置不同格式(XAML、RTF 和纯文本)的文本。
Here is the link: Extended WPF Toolkit RichTextBox
回答by Curtis
How about just doing the following:
只需执行以下操作如何:
_richTextBox.SelectAll();
string myText = _richTextBox.Selection.Text;
回答by sma6871
to setRichTextBox text:
以一套RichTextBox的文字:
richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));
to getRichTextBox text:
要获取RichTextBox的文字:
string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
回答by Vincenzo Costa
RichTextBox rtf = new RichTextBox();
System.IO.MemoryStream stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(yourText));
rtf.Selection.Load(stream, DataFormats.Rtf);
OR
或者
rtf.Selection.Text = yourText;
回答by Smile4ever
Using two extension methods, this becomes very easy:
使用两种扩展方法,这变得非常容易:
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;
}
}