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 BindableRun 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).
Essentially, I want to do this:
<FlowDocument Name="FlowDoc" FontFamily="{x:Static SystemFonts.CaptionFontFamily}"
Background="{x:Static SystemColors.ControlBrush}"
FontSize="{x:Static SystemFonts.SmallCaptionFontSize}"
TextAlignment="Left">
<FlowDocument.Resources>
<Style TargetType="{x:Type local:BindableRun}">
<!-- 1. Style all BindableRuns with a pink background by default -->
<Setter Property="Background" Value="HotPink"/>
<Style.Triggers>
<!-- 2. Style BindableRuns where Text=Hello to have a green background-->
<DataTrigger Binding="{Binding BoundText}" Value="Hello">
<Setter Property="Background" Value="GreenYellow"/>
</DataTrigger>
<!-- 3. Fire a Datatrigger with parameterised converter to change
text to Orange if the text contains 'StackOverflow'-->
<DataTrigger Binding="{Binding BoundText,
Converter={StaticResource ContainsBoolConverter},
ConverterParameter=StackOverflow}" Value="False">
<Setter Property="Foreground" Value="Orange"/>
</DataTrigger>
</Style.Triggers>
</Style>
</FlowDocument.Resources>
</FlowDocument>
Now, when I try and use the Xaml above:
• 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
• The DataTriggers against the BountText property of the BindableRun objects don’t work.
• The converter isn’t even called (I’ve put a breakpoint on it) so it looks like the binding/datatrigger just isn’t firing.
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.
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 ;)).
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();
}
};

