2009-06-04 44 views
0

我有一個TreeView與分層數據模板綁定到可觀察集合。當集合完成加載時,我運行一些選擇特定TreeViewItem的代碼,然後將焦點帶入TreeView。這個想法是,用戶應該能夠使用箭頭鍵在TreeView中滾動條目。 TreeView在填充時似乎具有焦點,但arrtow鍵不會滾動選擇。如果隨後在任何地方單擊TreeView,那麼箭頭鍵將起作用,這表明TreeView實際上並沒有關注。相關的代碼是:Silverlight 3 - 樹視圖沒有得到重點

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(); 
      }); 

任何幫助將不勝感激。

回答

0

我有這樣的代碼在XAML:

<ScrollViewer Name="_treeScroll"> 
      <controls:TreeView x:Name="_tree"/> 
</ScrollViewer> 

,當我想擴大一些treeItem。我跑這個

private void ExpandTreeViewItem(TreeViewItem tvi) 
     { 
      tvi.IsSelected = true; 
      tvi.IsExpanded = true; 
      tvi.UpdateLayout(); 
      _treeScroll.ScrollIntoView(tvi); 
     } 

希望它幫助...