开发者

How to highlight search results in WPF treeview?

I am developing an app ( A database file system ). I am using WPF treeview in its GUI.The treeview items are directly extracted from my apps database.开发者_开发问答

I want to search and highlight the treeview items as I type. To understand the question better, consider the keyword search results shown in browsers.


alt text http://img100.imageshack.us/img100/4168/chromel.jpg

How do I achieve that in my context?


You can do this with a combination of the "KeyDown" event on whatever control the user types into.

And this ......

Highlight whole treeview lines


Use a view model, wrap all the items that appear in your tree view. Give them all a IsHighlighted property. Perform your search on the view model items, if they match the criterion, set your IsHighlighted property to true. Use a trigger on your treeviewitem style that is bound to the IsHighlighted property, that updates the colour of the background.

The treeview can only have one selected item at once (it does not support multiple selection like the list view) So if you want to have more than one search result, setting the SelectedItem of the treeview wont be enough. Using a view model which has properties such as IsExpanded can also allow you to do some funky stuff. e.g. if a node is 'found' you can expand all the other nodes above it to make it visible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜