I've got a TreeView with a Hierarchical Data Template bound to an observable collection. When the collection is done loading, I run some code that selects a specific TreeViewItem and should then bring focus to the TreeView. The idea is that the user should be able to use the arrow keys to scroll through entries in the TreeView. The TreeView appears to have focus when it is populated, but the arrtow keys will not scroll the selection. If the TreeView is then clicked anywhere, the arrow keys then work, whihc suggests that the TreeView does not, in fact, have focus. The relevant code is:
Dispatcher.BeginInvoke(delegate
{
TreeViewAutomationPeer trvAutomation =
(TreeViewAutomationPeer)TreeViewAutomationPeer.CreatePeerForElement(TreeViewIndex);
IScrollProvider scrollingAutomationProvider =
(IScrollProvider)trvAutomation.GetPattern(PatternInterface.Scroll);
IndexEntry highScoreEntry = _currentIndexViewModel.HighScoreEntry;
TreeViewItem myItem = TreeViewIndex.ContainerFromItem(highScoreEntry);
TreeViewIndex.Focus();
myItem.IsSelected = true;
if (scrollingAutomationProvider.HorizontallyScrollable)
scrollingAutomationProvider.SetScrollPercent(0, scrollingAutomationProvider.VerticalScrollPercent);
TreeViewIndex.Focus();
});
Any help would be much appreciated.