views:

22

answers:

1

I'd like to display search results within a WPF ItemsControl with the query terms highlighted.

The search engine I use, Lucene.Net with the Highlighter plugin, returns strings with the query terms marked up like so:

...these <Bold>results</Bold> were found to be statistically significant...

I can instruct the Highlighter plugin to use any set of markup tags to wrap a query term. I'm not limited to the <Bold> tag in the example above. For WPF, I'd likely make these <Run/> elements with a style attached.

The challenge is to take the string I've been given and render it as if it were "actual XAML" within the datatemplate I'm using for search results. In other words, I want to see something like this:

...these results were were found to be statistically significant...

But I'm struggling with how to combine databinding with dynamic rendering of an XAML string within the datatemplate. What's the best approach here?

  1. Use a UserControl to display each search result and call XamlReader.Load() from the codebehind?
  2. Construct a FlowDocument containing the search result strings and display the results with a FlowDocumentScrollViewer?
  3. Something else entirely...?
A: 

A TextBlock can contain multiple Runs in its Inlines collection. You can build it in code or in XAML:

<TextBlock>
    <Run>... these </Run>
    <Run FontWeight="Bold">results</Run>
    <Run> were found...</Run>
</TextBlock>
Mart
My question probably wasn't clear enough. The tricky part is that I need to change the string into XAML at run time, not compile time.
dthrasher
Maybe I missed something but is looks feasible to me to build an XAML string like in my example (with some regular expression) and use your first approach. The solution I suggested was to build a TextBlock at runtime and populate its Inlines collection with Runs. Adding a style to the highlighted Runs is a solution to replace FontWeight="Bold".
Mart