Search code examples
wpfmultithreadingvisual-tree

Walk the VisualTree in a worker thread for fulltext search


I am using WPF to show complex data (think reporting). I now need to have a fulltext search for it. We currently do this by walking the visual tree looking for textblocks. It seems that this needs to be done on the UI thread, is that right? The problem here is that it might take quite a while until the next match is found during wich the whole UI blocks.

Is there a way to circumvent this problem? I could try to use UI Automation but once I found a match how do I highlight it? Using UI Automation I do not get references to the actual object instance of the UI Element, hence I can not put an adorner on top of it.

How is fulltext search in WPF usually implemented? How does the XPS viewer do it?


Solution

  • You are correct in that querying the visual tree will have to be done on the UI thread, and also that UI Automation can only interact with an application in a similar way to a person sitting at the computer, so it doesn't have access to the actual controls (just as the person doesn't).

    The correct way to approach this is to put the UI aside and search for text in your data source (the model in MVVM) instead. After all, you really want to search the information displayed by your UI and not the UI itself.