TreeView virtualization Jump

Using the ItemContainerGenerator .ContainerFromItem UIElement method to obtain the corresponding data.

But if you use virtualization technology, beyond the visible area to get UIElement unlikely.

Refer to the Microsoft documentation , " How to: Find the TreeView TreeViewItem " , remove unnecessary and outdated code, write a complete example.

Only five major lines of code

1                 itemsControl.ApplyTemplate();
2                 var itemsPresenter = FindVisualChild<ItemsPresenter>(itemsControl);
3                 itemsPresenter.ApplyTemplate();
4                 var virtualizingStackPanel = (VirtualizingStackPanel)VisualTreeHelper.GetChild(itemsPresenter, 0);
5                 virtualizingStackPanel.BringIndexIntoViewPublic(data.index);

By such processing, it can be taken to UIElement.

                itemsControl = (ItemsControl)itemsControl.ItemContainerGenerator.ContainerFromItem(vm);

Demo run renderings

Complete example in my Github in

Guess you like

Origin www.cnblogs.com/noctwolf/p/11123038.html