有什么方法可以同时搜索两个 FlowDocuments 吗?

Any way to search two FlowDocuments at the same time?

我在两个不同的堆栈面板中并排放置了两个 FlowDocuments。我需要一种方法来同时搜索两个文档以查找特定文本。就像我在文本框中键入 "car" 一样,流文档 reader 应该搜索并滚动到 "car" 的下一个实例(如果有的话)。有什么办法可以做到这一点? FlowDocument 在 FlowDocumentReader 中。

这里我有一个基本的 WPF XAML 布局,其中包含您指定的 2 个 FlowDocumentReader。我有一个搜索文本框,每当搜索文本发生变化时,我都会 运行 后面的代码:

<Window x:Class="WpfFlowTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfFlowTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

  <DockPanel>
    <!-- Search Box -->
    <TextBox Name="SearchTextBox" DockPanel.Dock="Top" TextChanged="TextBox_TextChanged"/>

    <!-- 2 Flow Readers -->
    <UniformGrid Columns="2">
      <FlowDocumentReader Name="FlowReader1">
        <FlowDocument>
          <Paragraph>
            Here is some text in panel number 1
          </Paragraph>
        </FlowDocument>
      </FlowDocumentReader>

      <FlowDocumentReader Name="FlowReader2">
        <FlowDocument>
          <Paragraph>
            Here is some more text in panel number 2
          </Paragraph>
        </FlowDocument>
      </FlowDocumentReader>
    </UniformGrid>
  </DockPanel>
</Window>

在 MainWindow.xaml.cs 文件中,我有这段代码将突出显示文本与您输入的内容匹配的流程文档:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var searchText = SearchTextBox.Text;

            DoSearch(FlowReader1, searchText);
            DoSearch(FlowReader2, searchText);
        }

        private void DoSearch(FlowDocumentReader reader, string search)
        {
            var doc = reader.Document;
            var text = doc.ContentStart;

            var docRange = new TextRange(doc.ContentStart, doc.ContentEnd);
            docRange.ClearAllProperties();

            while (true)
            {
                var next = text.GetNextContextPosition(LogicalDirection.Forward);
                if (next == null)
                {
                    break;
                }

                var txt = new TextRange(text, next);

                int indx = txt.Text.IndexOf(search);
                if (indx > 0)
                {
                    var sta = text.GetPositionAtOffset(indx);
                    var end = text.GetPositionAtOffset(indx + search.Length);
                    var textR = new TextRange(sta, end);

                    // Make it yellow
                    textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
                }
                text = next;
            }

        }

我已经为想要滚动的人发布了我的细微修改。

private void TextBox_TextChanged(object sender, EventArgs e)
        {
            var searchText = SearchTextBox.Text;

            if (searchText != null || searchText != "")
            {
                var FlowReader1 = (FlowDocumentReader)diffResults.Children[0];
                var FlowReader2 = (FlowDocumentReader)oldResults.Children[0];

                DoSearch(FlowReader1, searchText);
                DoSearch(FlowReader2, searchText);
            }
        }

        private void DoSearch(FlowDocumentReader reader, string search)
        {
            bool toScroll = true;
            var doc = reader.Document;
            var text = doc.ContentStart;

            var docRange = new TextRange(doc.ContentStart, doc.ContentEnd);
            docRange.ClearAllProperties();

            while (true)
            {
                var next = text.GetNextContextPosition(LogicalDirection.Forward);
                if (next == null)
                {
                    break;
                }

                var txt = new TextRange(text, next);

                int indx = txt.Text.IndexOf(search);
                if (indx >= 0)
                {   
                    var sta = text.GetPositionAtOffset(indx);
                    var end = text.GetPositionAtOffset(indx + search.Length);
                    if (end == null)
                    {
                        end = text.GetPositionAtOffset(indx + 1);
                    }
                    var textR = new TextRange(sta, end);

                    if (toScroll && text.Paragraph != null)
                    {
                        text.Paragraph.BringIntoView();
                        toScroll = false;
                    }
                    // Make it yellow
                    textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
                }
                text = next;
            }