Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and highlight any text on a WPF Window with many type of controls

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. Find all controls in WPF Window by type
  2. 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
like image 502
Vin Avatar asked Oct 14 '22 14:10

Vin


1 Answers

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.

like image 171
Vin Avatar answered Oct 26 '22 22:10

Vin