views:

733

answers:

1

This is basically implementing search/find in a WPF window or User control. Let's say you have a WPF window with dynamic readonly text some on textblock, some in a listbox, some in treeview, some in textblocks etc.

The user should be able to type in a search term in a textbox on the top and be able to see it highlighted wherever and in whichever control the text appears in the window. And no, this page has nothing to do with FlowDocuments and such, so can't use out of the box FlowDocumentReader.

Call it 'control text search' and highlight the search term like Google chrome browser does for a webpage.

The following SO threads might be helpful, but not sure if I can achieve the above (I am trying to put something together)

  1. http://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type
  2. http://stackoverflow.com/questions/798579/wpf-listbox-highlight-part-of-listboxitem-element

There there is this article, which is also helpful but works with Documents and not controls

  1. Search and highlight any text on WPF rendered page
+2  A: 

Alright I seem to have found a link that seems like what I was after.

Search and Highlight Text in an Arbitrary Visual Tree

I am going to go through this, will come back and update if it's the complete solution to the problem above

Update 1: Okay, this really works, the above link has code that actually solves this problem, great work there Kent !!! - It highlights all search text with yellow all at once, but one thing that is missing is the navigation highlighting, like next->next. I am planning to work on that and then I can post my updates here, so that it helps others.

Update 2: Alright there is another catch with this solution. The highlighting rectangles are drawn on the screen co-ordinates. So if you have long list of items, and you scroll, the highlighted rectangles still stick to the screen co-ords, as opposed to the textblocks themselves.

So now my teammate and I are going to be working on a different solution. Will keep this thread posted.

Vin
Thanks for posting this... I was just about to embark on this myself. Now I don't have to!
Anderson Imes
Sure glad it helped
Vin