开发者

How to programmatically read the paragraph values from the richtexteditor?

开发者 https://www.devze.com 2023-03-11 01:36 出处:网络
<RichTextBox HorizontalAlignment=\"Left\" Margin=\"21,92,0,0\" Name=\"richTextBox1\" Vertical开发者_StackOverflowAlignment=\"Top\" Height=\"259\" Width=\"357\" >
 <RichTextBox HorizontalAlignment="Left" Margin="21,92,0,0" Name="richTextBox1" Vertical开发者_StackOverflowAlignment="Top" Height="259" Width="357" >
            <Paragraph>hello kitty!</Paragraph>
            <Paragraph>hello world!</Paragraph>
            <Paragraph>hello fb!</Paragraph>
        </RichTextBox>


   BlockCollection bc = richTextBox1.Blocks;
            foreach (var b in bc)
            {

            }

The Paragraphs are in the collection and b are Paragraph type but I don't know how to read the value from them. There aren't any text property or innerHTML property.


You could use code like this to get values from the paragraphs.

foreach (var paragraph in richTextBox1.Blocks.OfType<Paragraph>())
{
    foreach (var run in paragraph.Inlines.OfType<Run>())
    {
        var text = run.Text;
    }
}


The paragraph does not contain text directly instead it contains collection of inlines: runs, spans, hyperlinks, etc. Paragraph in you example contain single run each. To get text from the paragraph you need to "flatten" it first. There is two ways to do it: do it yourself as Alex sugested or using selection. Set selection from start of the paragraph to the end of it and use Text property of selection, it will do flattening for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消