Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TreeView - How to scroll so expanded branch is visible

When I expand items in my treeview so that scrolling is necessary, a scrollbar appears. However, it doesn't scroll down for the newly expanded branch of items - they get cropped by the bottom of the control. So as I continue expanding items at the bottom of the tree, I have to keep manually scrolling down to see the new children. Anyone have a suggestion for how make it automatically scroll to show the newly expanded items?

like image 887
Jared Avatar asked Feb 11 '10 00:02

Jared


1 Answers

You can use a simple EventSetter in TreeViewItem style to invoke an event handler when the item is selected. Then call BringIntoView for the item.

<TreeView >  <TreeView.ItemContainerStyle>    <Style TargetType="{x:Type TreeViewItem}">      <EventSetter Event="Selected" Handler="TreeViewSelectedItemChanged" />    </Style>  </TreeView.ItemContainerStyle>  </TreeView>  private void TreeViewSelectedItemChanged(object sender, RoutedEventArgs e) {     TreeViewItem item = sender as TreeViewItem;     if (item != null)     {         item.BringIntoView();         e.Handled = true;       } } 
like image 81
Vinod.M.S Avatar answered Sep 22 '22 03:09

Vinod.M.S