2016-07-19 5 views
0

2つの異なるスタックパネルに2つのフロードキュメントが並べてあります。私は特定のテキストのために両方のドキュメントを同時に検索する方法が必要です。私がテキストボックスに "car"とタイプする場合、フロードキュメントリーダーは両方とも必要であり、 "car"の次のインスタンスがあればそれを検索してスクロールする必要があります。これを達成する方法はありますか? FlowDocumentはFlowDocumentReaderの内部にあります。同時に2つのFlowDocumentsを検索する方法はありますか?

答えて

3

ここでは、指定した2つのFlowDocumentReadersを持つ基本的なWPF XAMLレイアウトがあります。私は、検索テキストボックスを持っており、いつでも検索テキストの変更の後ろにコードを実行するつもりです:MainWindow.xaml.csファイルで

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

私は、テキストが何一致するフローの文書をハイライト表示します。このコードを持っています入力された:

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

     } 
+0

これはかなり良い解決策です。 – jailorboy

+0

文書を強調表示されたテキストにスクロールする方法はありますか? – jailorboy

+0

私の知る限りでは、文書を強調表示されたテキストにスクロールする組み込みの方法はありませんが、 'LineHeight'、' PagePadding'、その他の余白やマージン(スペーシング段落の間に)。 – Meloviz

0

私はスクロールしたい人のための私の若干の修正を掲載しました。

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

'BringIntoView()は実際に' Viewport'に持ち込みますが、必ずしも 'Viewport'のプリセット位置に持ってくるわけではありません。私はあなたが 'Viewport'の最上部でそれを望んでいると仮定したので、それは私の悪です。しかし、あなたがそのテキストの最上部にテキストを持っていこうと思っているなら、そこに何かがないかぎり、あなたは実際にそれを計算しなければなりません。それは私がしなければならなかったことです。 – Meloviz

+0

ええ、私はそれがユーザーに見えるようにしたかっただけです。 – jailorboy

関連する問題