WPF TreeView - How to scroll so expanded branch is visible

前端 未结 6 1794
孤城傲影
孤城傲影 2020-12-14 16:56

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 cr

6条回答
  •  鱼传尺愫
    2020-12-14 17:21

    Thanks to itowlson's answer, here's the expanded event handler code that works for both of my trees

    private static void Tree_Expanded(object sender, RoutedEventArgs e)
    {
        // ignore checking, assume original source is treeviewitem
        var treeViewItem = (TreeViewItem)e.OriginalSource;
    
        var count = VisualTreeHelper.GetChildrenCount(treeViewItem);
    
        for (int i = count - 1; i >= 0; --i)
        {
            var childItem = VisualTreeHelper.GetChild(treeViewItem, i);
            ((FrameworkElement)childItem).BringIntoView();
        }
    
        // do NOT call BringIntoView on the actual treeviewitem - this negates everything
        //treeViewItem.BringIntoView();
    }
    

提交回复
热议问题