views:

757

answers:

0

I was able to follow Justin Angel's (Silverlight team member) advice from this question to scroll to a particular item in a TreeView. Unfortunately, it simply isn't working in my project.

Based on Justin's advice, I implemented the extension methods he designed which allowed me to get a TreeViewItem from an underlying object and in tracing through my code, this works perfectly. Once I've gotten the TreeViewItem as a first pass, I simply tried selecting the item, which should then cause the TreeView to scroll to that item. After working back & forth with the code, I was able to establish that there is a timing issue. The basic workflow is as follows:

  1. User enters a search term.
  2. A request is made of a webservice based on that term.
  3. Web service returns XML
  4. XML is processed into Business Objects
  5. Collection of Business Objects is returned to the ViewModel.
  6. Collection is added to an ObservableCollection that is bound to the TreeView and an event is fired to notify the view that the ObservableCollection has been populated.
  7. An event handler runs that gets the appropriate TreeViewItem and sets the IsSelected property to TRUE, which should then cause the TreeView to scroll to that item, making it visible.

It appears that after step 7, the TreeView finishes binding, loading and rendering all the TreeView items. Once that occurs, the TreeView scrolls to the bottom of the list, which then undoes the work performed in step 7. The items selected in step 7 is still selected, but the TreeView has scrolled to the bottom and scrolled the selected item out of view. I have been able to confirm this by adding a Timer to the process and waiting a second or two before executing the selection process and this works perfectly when I wait long enough to ensure that the TreeView is populated first.

I can't find an event or other mechanism to use to tell me when the TreeView is done binding & loading all the items so that I can time this process more effectively. I do have a fairly minimal and relevant reproduction of the issue and can send the project to anyone that wants to see it. Any help would be much appreciated.