title: Colour-coding text runs in a FlowDocument
body: <p>I’m trying to produce a FlowDocument, and want certain text runs to be highlighted when they contain a particular string. I’m using the <a href="http://www.fortes.com/2007/bindablerun" rel="nofollow">BindableRun</a> class to ensure I can update the Run text values (although I only set them at construction, so a vanilla instance of Run should be okay).</p>
  
  <p>Essentially, I want to do this:</p>
  
  <pre><code> &lt;FlowDocument Name="FlowDoc" FontFamily="{x:Static SystemFonts.CaptionFontFamily}" 
                    Background="{x:Static SystemColors.ControlBrush}" 
                    FontSize="{x:Static SystemFonts.SmallCaptionFontSize}"
                    TextAlignment="Left"&gt;
        &lt;FlowDocument.Resources&gt;
          &lt;Style TargetType="{x:Type local:BindableRun}"&gt;
            &lt;!-- 1. Style all BindableRuns with a pink background by default --&gt;
            &lt;Setter Property="Background" Value="HotPink"/&gt;
  
            &lt;Style.Triggers&gt;
  
              &lt;!-- 2. Style BindableRuns where Text=Hello to have a green background--&gt;
              &lt;DataTrigger Binding="{Binding BoundText}" Value="Hello"&gt;
                &lt;Setter Property="Background" Value="GreenYellow"/&gt;
              &lt;/DataTrigger&gt;
              &lt;!-- 3. Fire a Datatrigger with parameterised converter to change 
                    text to Orange if the text contains 'StackOverflow'--&gt;
              &lt;DataTrigger Binding="{Binding BoundText, 
                             Converter={StaticResource ContainsBoolConverter},
                             ConverterParameter=StackOverflow}" Value="False"&gt;
                &lt;Setter Property="Foreground" Value="Orange"/&gt;
              &lt;/DataTrigger&gt;
            &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
        &lt;/FlowDocument.Resources&gt;
      &lt;/FlowDocument&gt;
  </code></pre>
  
  <p>Now, when I try and use the Xaml above:</p>
  
  <p>•   The style to change the background of the BindableRun objects (i.e., #1) works fine – so the contents of the FlowDocument are what I’m expecting</p>
  
  <p>•   The DataTriggers against the BountText property of the BindableRun objects don’t work. </p>
  
  <p>•   The converter isn’t even called (I’ve put a breakpoint on it) so it looks like the binding/datatrigger just isn’t firing.</p>
  
  <p>Anyone done anything like this before? If so, have you managed to get it to work? There’s not so many FlowDocument examples on SO etc so I’ve not managed to track down anyone else who’s attempted (either successfully or not) to use DataTriggers on the Run's Text property.</p>
  
  <p>For reference, here's the Contains converter that I'm using (although it never fires at all, so it doesn't matter if it's right or not ;)).</p>
  
  <pre><code>public class ContainsTextConverter : IValueConverter
  {
      public object Convert( object value, Type t, object parameter, CultureInfo culture )
      {
          String input = value.ToString();
          string param = parameter.ToString();
  
          return input.IndexOf( param, StringComparison.OrdinalIgnoreCase ) != -1;
      }
  
      public object ConvertBack( object value, Type t, object parameter, CultureInfo culture )
      {
          // Don't care about this
          throw new NotSupportedException();
      }
  };
  </code></pre>