【问题标题】:How to retrieve RichTextBox content with the controls inside of it(buttons)?如何使用其中的控件(按钮)检索 RichTextBox 内容?
【发布时间】:2017-05-06 07:40:09
【问题描述】:

我正面临使用 RichTextBox 控件的挑战:

我可以在设计时成功地添加段落和按钮,请参见下面的 xaml:

<RichTextBox x:Name="rtxtStep" HorizontalAlignment="Left" Height="207" Margin="10,32,0,0" VerticalAlignment="Top" Width="427" IsDocumentEnabled="True" KeyUp="richTextBox_KeyUp">
            <FlowDocument>
                <Section FontSize="15">
                    <Paragraph>
                        Click on this:

                            <Hyperlin k  NavigateUri="http://stackoverflow.com">stackoverflow</Hyperlin k>


                    </Paragraph>

                    <Paragraph>
                        <Button Click="Button_Click" Width="143" >Also Click On This</Button>
                        <Button Click="Button_Click" Width="143" >button 2</Button>
                    </Paragraph>
                </Section>
            </FlowDocument>
        </RichTextBox>

而且我能够从我的代码中检索文本就好了,见下文:

    private void richTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            TextRange txtrContent = new TextRange(rtxtStep.Document.ContentStart, rtxtStep.Document.ContentEnd);
            string allContent = txtrContent.Text;
        }
    }

返回:

"Click on this: stackoverflow\r\n   \r\n\r\n"

问题是,我如何检索按钮和文本?

【问题讨论】:

    标签: wpf richtextbox


    【解决方案1】:

    我觉得你真的期望太高了。 RichTextBox 中的 FlowDocument 是元素的层次结构。您可以做的最好的事情是沿着元素树向下查找按钮。像这样的...

        private void richTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextRange txtrContent = new TextRange(rtxtStep.Document.ContentStart, rtxtStep.Document.ContentEnd);
                string allContent = txtrContent.Text;
                PrintBlocks(rtxtStep.Document.Blocks);
            }
        }
    
        private void PrintBlocks(IEnumerable<Block> blocks)
        {
            foreach(Block b in blocks)
            {
                Trace.WriteLine("Found " + b.GetType().Name);
                if(b is Section)
                {
                    PrintBlocks((b as Section).Blocks);
                }
                else if(b is Paragraph)
                {
                    PrintInlines((b as Paragraph).Inlines);
                }
            }
    
        }
    
        private void PrintInlines(IEnumerable<Inline> inlines)
        {
            foreach(Inline i in inlines)
            {
                if(i is InlineUIContainer)
                {
                    PrintInlineUIContainer(i as InlineUIContainer);
                }
    
            }
        }
        private void PrintInlineUIContainer(InlineUIContainer i)
        {
            Trace.WriteLine("Found " + i.Child.GetType().Name + " " + i.Child.ToString());
        }
    

    对于您的 XAML,会生成此输出...

    Found Section
    Found Paragraph 
    Found Paragraph
    Found Button System.Windows.Controls.Button: Also Click On This
    Found Button System.Windows.Controls.Button: button 2
    

    但是你已经知道了。您似乎想要某种集成格式的文本和按钮(可能是 HTML?)。我认为您需要编写自己的代码来执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 2021-09-30
      • 2019-08-01
      • 2014-02-11
      • 2011-02-07
      • 1970-01-01
      相关资源
      最近更新 更多