简体   繁体   中英

TreeItem Scroll to selected item

Does anyone has an idea of how to scroll a TreeItem to the selected item. I have been googling but not clear results worked for me. I tried BringIntoView() but it is not working...

A simple solution is to use a behaviour for binding to selected item, see: Data binding to SelectedItem in a WPF Treeview . Modify the OnSelectedItemPropertyChanged to something like:

private static void OnSelectedItemPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
  ((BindableSelectedItemBehavior) sender).OnSelectedItemChanged(e.NewValue);
}

private void OnSelectedItemChanged(object newValue)
{
  var treeViewItem = AssociatedObject.ItemContainerGenerator.ContainerFromItem(newValue) as TreeViewItem;
  treeViewItem.SetValue(TreeViewItem.IsSelectedProperty, true);
  treeViewItem.BringIntoView();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM