Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TreeView - scroll expanded items into view

Tags:

wpf

treeview

I'm trying to write a custom WPF/MVVM TreeView control that will automatically scroll (as many as possible) of the child items into view when a parent item is expanded.

I've found this post, WPF TreeView - How to scroll so expanded branch is visible, but it seems that only works when the tree items actually descend from TreeViewItem.

My Tree Item class is just a plain C# object - along the lines of Josh Smith's blog http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode, which are lazy loaded in the IsExpanded property setter in the Tree Item class.

Stepping into the OnExpand event handler, I can see that the original source is a TreeViewItem (created by the framework behind the scenes) which has my tree item object in the header field. This TreeViewItem has only 1 visual child, a grid, which in turn has 3 visual children, ToggleButton, Border and ItemsPresenter.

Does anyone have a hint as to how I can get the corresponding visual components for my child items, on which I can then call BringIntoView()?

like image 782
Peregrine Avatar asked Sep 01 '26 11:09

Peregrine


1 Answers

Found a very nice behavior which scrolls the selected item into view, located here:

This unfortunately doesnt cater for when the item is expanded, but can be modifed according to your exact requirements. If you need any help just let me know.

http://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF

like image 87
AGhosT Avatar answered Sep 03 '26 07:09

AGhosT