0
votes

I have a WPF treeview control that uses virtualization owing to the size of my datasets. The hierachy is Customer -> Account -> Transaction with an example dataset of 150 customers, most of which just have one account and transaction, but four have 8000 transactions.

I need to have search functionality to enter a transaction id and to navigate to that place in the tree view. Currently I am using an MVVM model and so searching for a transaction is trivial and when I have the node ViewModel back, I can just call SelectThis() and it works fine. But the problem is it does not bring the selected node into the viewable area.

I have got a function that takes a ViewModel object and traverses the TreeView until it finds the TreeViewItem that contains the ViewModel, it generates the container and then calls BringIntoView() on the TreeViewItem.

The first time you search a transaction it works fine. The second time for a different transaction ID, it freezes. If you try searching for the second ID first, it works fine, its always when you try the operation twice it fails.

I dont really understand what WPF is doing behind the scenes but I'm guessing my code is doing something with generating containers that accumulate and end up crashing the code as there's too many. When I break all in the debugger, it seems to be constantly going aroudn and aroudn retrieving the bound dependency properties for each Transaction object, so not sure why it's trying to get so many of them when there are only 30 in the viewable area, it just seems to be trying to retrieve every single one.

Is there a way in between calls to Search for Transaction that I can clear all generated UI containers and objects from the TreeView control and reset it to unloaded so every search starts from scratch with regards to generating containers?

Thanks

1

1 Answers

0
votes

You can use ICollectionView to do a search on the treeview by binding the TreeView's Itemsource to collection view as shown below

 ICollectionView defaultView = CollectionViewSource.GetDefaultView(treeView.ItemsSource);
 defaultView.Filter += delegate(object item)
 {
    string searchString = searchTextBox.Text;
 }

Link for further help

One more Reference