简体   繁体   English

RichTextBox(WPF)没有字符串属性“Text”

[英]RichTextBox (WPF) does not have string property “Text”

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 ? 你知道怎么可能吗?

to set RichTextBox text: 设置 RichTextBox文本:

richTextBox1.Document.Blocks.Clear();
richTextBox1.Document.Blocks.Add(new Paragraph(new Run("Text")));

to get RichTextBox text: 获取 RichTextBox文本:

string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;

There was a confusion between RichTextBox in System.Windows.Forms and in System.Windows.Control 有RichTextBox中之间的混淆System.Windows.Forms的System.Windows.Control

I am using the one in the Control as I am using WPF. 我正在使用控件中的那个,因为我正在使用WPF。 In there, there is no Text property, and in order to get a text, I should have used this line: 在那里,没有Text属性,为了获得文本,我应该使用这一行:

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

thanks 谢谢

The WPF RichTextBox has a Document property for setting the content a la MSDN: 在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;

You can just use the AppendText method though if that's all you're after. 你可以使用AppendText方法,如果这就是你所追求的全部。

Hope that helps. 希望有所帮助。

string GetString(RichTextBox rtb)
{
    var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    return textRange.Text;
}

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

There is no Text property in the WPF RichTextBox control. WPF RichTextBox控件中没有Text属性。 Here is one way to get all of the text out: 这是获取所有文本的一种方法:

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

string allText = range.Text;

How about just doing the following: 如何做到以下几点:

_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);

OR 要么

rtf.Selection.Text = yourText;

"Extended WPF Toolkit" now provides a richtextbox with the Text property. “Extended WPF Toolkit”现在提供带有Text属性的richtextbox。

You can get or set the text in different formats (XAML, RTF and plaintext). 您可以以不同的格式(XAML,RTF和纯文本)获取或设置文本。

Here is the link: Extended WPF Toolkit RichTextBox 这是链接: 扩展WPF工具包RichTextBox

In my case, I had to turn a RTF text to plain text: what I did (using Xceed WPF Toolkit) like in GiangLP answer ; 在我的情况下,我不得不将RTF文本转换为纯文本:我在GiangLP答案中所做的(使用Xceed WPF Toolkit); and setting my code in an extension method : 并在扩展方法中设置我的代码:

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;

    }

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. 如果您希望将文本拆分为线条,也可以尝试“线条”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM