views:

83

answers:

2

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?

A: 

wpf automatically marshals inotifypropertychanged onto the ui thread so you can do your processing on another thread and set properties. how you do this depends on the structure of your code. for example are you using mvvm? if not you may meed to use the dispatcher to invoke any methods on your ui. if you reply with a bit more info about you code structure i should be able to provide a more difinitive answer. if you are not using mvvm you can still avoid explicitly marshaling if you use wpf databinding with a non ui datasource that you can do your processing on.

Steve Psaltis
I do not use MVVM, I generate the UI in c# based on some model data every time I have read in the model data. I start to search the VisualTree for TextBoxes upon mouse click. Acessing the visual tree and dependency properties can not done be in another thread than the UI Thread.
bitbonk
+1  A: 

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.

GraemeF
I need to highlight the found text. Using the MVVM approach, how would I go about selecting/highlight the textparts that have been found by/in the ViewModel? There must be some sort of connection between all the various text parts and the ViewModel instance. Also using MVVM , the VIewModel needs to know about each single text that is used in the View. `<StackPanel><Label>Value: </Label><TextBox Text="{Binding MyViewModelValueProperty}"/></StackPanel>` Here the label content is only known by the view but it needs to be found by the text search too.
bitbonk
To do it with MVVM you would have to make sure that everything you want to search is represented in your model, and you would have to come up with a mechanism to relate the model to the UI for highlighting (e.g. proving start character and length of the highlight). If you're totally convinced that it's the UI you need to search then you will have no option but to use the UI thread to query the contents of the controls and do the highlighting. UI Automation won't let you modify anything that the user couldn't change.
GraemeF
Wouldn't UI Automation give the bounding box of the found control? I might be able to add a adorner in screen coordinates ... (?)
bitbonk
Yes, but it would be extremely brittle - what if the user moved the window, etc.
GraemeF