Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to iterate using <code>GetNextContextPosition(LogicalDirection.Forward)</code> and get <code>TextPointer</code>, use this one with previous <code>TextPointer</code> to construct <code>TextRange</code>. On this <code>TextRange</code> you can apply your logic.</p> <p>What you can't do is use single TextRange from <code>FlowDocument</code> for searching text. <code>FlowDocument</code> is not only text:</p> <pre><code> private void Button_Click(object sender, RoutedEventArgs e) { String search = this.content.Text; TextPointer text = doc.ContentStart; while (true) { TextPointer next = text.GetNextContextPosition(LogicalDirection.Forward); if (next == null) { break; } TextRange txt = new TextRange(text, next); int indx = txt.Text.IndexOf(search); if (indx &gt; 0) { TextPointer sta = text.GetPositionAtOffset(indx); TextPointer end = text.GetPositionAtOffset(indx + search.Length); TextRange textR = new TextRange(sta, end); textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow)); } text = next; } } </code></pre> <p>UPDATE: it does not work allways, for example if you have list, special characters (\t) are counted by <code>IndexOf</code>, but <code>GetPositionAtOffset</code> doesn't count them in:</p> <pre><code>• ListItem 1 • ListItem 2 • ListItem 3 </code></pre> <p>this line:</p> <pre><code>int indx = txt.Text.IndexOf(search); </code></pre> <p>could be replaced with:</p> <pre><code>int indx = Regex.Replace(txt.Text, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled).IndexOf(search); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload