我如何从 RichTextBox 读取带有信息的内容是粗体、下划线、斜体等 [英] How I can read contents from RichTextBox with information is it bolded, underlined, italic etc

查看:65
本文介绍了我如何从 RichTextBox 读取带有信息的内容是粗体、下划线、斜体等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚如果文本是粗体、下划线或斜体,如何从富文本框中获取信息.因此,如果我从richtextbox 中读取一行,我需要知道某些单词是否加粗等?我不想将内容保存到 .rtf 而是另一个,所以这就是为什么我需要知道哪些单词被格式化为某些东西,以便我可以在单词之前添加标签以将内容保存到 .txt 文件.

Ive been trying to figure out how to obtain information from richtextbox if text is bolded, underlined or italic. So if I read a line from richtextbox, I need to know if some of the words is bolded etc? I dont want to save the contents to .rtf but to another, so that's why I need to know which words are formatted to something so I can add tags before the word to save contents like to .txt file.

是的,如果你这样做就行了

Yes, it works if you make it like this

private void Button_Click(object sender, RoutedEventArgs e)
{
    foreach (Paragraph p in myRichTextBox.Document.Blocks)
    {
        foreach (var inline in p.Inlines)
        {
            if (inline.FontWeight == FontWeights.Bold)
            {
                // obtain text from p
            }               
        }
    }
}

如何获取粗体文本?

推荐答案

WPF 中的 RTB 包含 FlowDocument.因此,您可以解析文档的树,并检测其中 inline 包含特定文本.基本概念:

RTB in WPF contains FlowDocument. Hence, you can parse document's tree, and detect, which inline contains particular text. The basic concept:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <RichTextBox x:Name="myRichTextBox">
        <RichTextBox.Document>
            <FlowDocument>
                <Paragraph>
                    <Bold>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</Bold>
                </Paragraph>
                <Paragraph>
                    <Italic>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</Italic>
                </Paragraph>
                <Paragraph>
                    <Underline>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</Underline>
                </Paragraph>
            </FlowDocument>
        </RichTextBox.Document>
    </RichTextBox>

    <Button Grid.Row="1" Content="Parse" Click="Button_Click"/>
</Grid>

代码隐藏:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        foreach (Paragraph p in myRichTextBox.Document.Blocks)
        {
            foreach (var inline in p.Inlines)
            {
                if (inline is Bold)
                {
                    // ...
                }

                if (inline is Italic)
                {
                    // ...
                }

                if (inline is Underline)
                {
                    // ...
                }
            }
        }
    }

注意,内联可以嵌套:

                    <Underline>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                        <Bold>
                            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                            <Italic>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                            </Italic>
                        </Bold>
                    </Underline>

您在解析内容时应该考虑到这一点.

You should take this into consideration, when parsing content.

这篇关于我如何从 RichTextBox 读取带有信息的内容是粗体、下划线、斜体等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆