Selecting a node in virtualized TreeView with WPF

13,020

Solution 1

The link Estifanos Kidane gave is broken. He probably meant the "Changing selection in a virtualized TreeView" MSDN sample. however, this sample shows how to select a node in a tree, but using code-behind and not MVVM and binding, so it also doesn't handle the missing SelectedItemChanged event when the bound SelectedItem is changed.

The only solution I can think of is to break the MVVM pattern, and when the ViewModel property that is bound to SelectedItem property changes, get the View and call a code-behind method (similar to the MSDN sample) that makes sure the new value is actually selected in the tree.

Here is the code I wrote to handle it. Suppose your data items are of type Node which has a Parent property:

public class Node
{
    public Node Parent { get; set; }
}

I wrote the following behavior class:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

public class NodeTreeSelectionBehavior : Behavior<TreeView>
{
    public Node SelectedItem
    {
        get { return (Node)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(Node), typeof(NodeTreeSelectionBehavior),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemChanged));

    private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var newNode = e.NewValue as Node;
        if (newNode == null) return;
        var behavior = (NodeTreeSelectionBehavior)d;
        var tree = behavior.AssociatedObject;

        var nodeDynasty = new List<Node> { newNode };
        var parent = newNode.Parent;
        while (parent != null)
        {
            nodeDynasty.Insert(0, parent);
            parent = parent.Parent;
        }

        var currentParent = tree as ItemsControl;
        foreach (var node in nodeDynasty)
        {
            // first try the easy way
            var newParent = currentParent.ItemContainerGenerator.ContainerFromItem(node) as TreeViewItem;
            if (newParent == null)
            {
                // if this failed, it's probably because of virtualization, and we will have to do it the hard way.
                // this code is influenced by TreeViewItem.ExpandRecursive decompiled code, and the MSDN sample at http://code.msdn.microsoft.com/Changing-selection-in-a-6a6242c8/sourcecode?fileId=18862&pathId=753647475
                // see also the question at http://stackoverflow.com/q/183636/46635
                currentParent.ApplyTemplate();
                var itemsPresenter = (ItemsPresenter)currentParent.Template.FindName("ItemsHost", currentParent);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    currentParent.UpdateLayout();
                }

                var virtualizingPanel = GetItemsHost(currentParent) as VirtualizingPanel;
                CallEnsureGenerator(virtualizingPanel);
                var index = currentParent.Items.IndexOf(node);
                if (index < 0)
                {
                    throw new InvalidOperationException("Node '" + node + "' cannot be fount in container");
                }
                CallBringIndexIntoView(virtualizingPanel, index);
                newParent = currentParent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
            }

            if (newParent == null)
            {
                throw new InvalidOperationException("Tree view item cannot be found or created for node '" + node + "'");
            }

            if (node == newNode)
            {
                newParent.IsSelected = true;
                newParent.BringIntoView();
                break;
            }

            newParent.IsExpanded = true;
            currentParent = newParent;
        }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
    }

    private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        SelectedItem = e.NewValue as Node;
    }

    #region Functions to get internal members using reflection

    // Some functionality we need is hidden in internal members, so we use reflection to get them

    #region ItemsControl.ItemsHost

    static readonly PropertyInfo ItemsHostPropertyInfo = typeof(ItemsControl).GetProperty("ItemsHost", BindingFlags.Instance | BindingFlags.NonPublic);

    private static Panel GetItemsHost(ItemsControl itemsControl)
    {
        Debug.Assert(itemsControl != null);
        return ItemsHostPropertyInfo.GetValue(itemsControl, null) as Panel;
    }

    #endregion ItemsControl.ItemsHost

    #region Panel.EnsureGenerator

    private static readonly MethodInfo EnsureGeneratorMethodInfo = typeof(Panel).GetMethod("EnsureGenerator", BindingFlags.Instance | BindingFlags.NonPublic);

    private static void CallEnsureGenerator(Panel panel)
    {
        Debug.Assert(panel != null);
        EnsureGeneratorMethodInfo.Invoke(panel, null);
    }

    #endregion Panel.EnsureGenerator

    #region VirtualizingPanel.BringIndexIntoView

    private static readonly MethodInfo BringIndexIntoViewMethodInfo = typeof(VirtualizingPanel).GetMethod("BringIndexIntoView", BindingFlags.Instance | BindingFlags.NonPublic);

    private static void CallBringIndexIntoView(VirtualizingPanel virtualizingPanel, int index)
    {
        Debug.Assert(virtualizingPanel != null);
        BringIndexIntoViewMethodInfo.Invoke(virtualizingPanel, new object[] { index });
    }

    #endregion VirtualizingPanel.BringIndexIntoView

    #endregion Functions to get internal members using reflection
}

With this class, you can write XAML like the following:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:local="clr-namespace:MyProject">
    <Grid>
        <TreeView ItemsSource="{Binding MyItems}"
                  ScrollViewer.CanContentScroll="True"
                  VirtualizingStackPanel.IsVirtualizing="True"
                  VirtualizingStackPanel.VirtualizationMode="Recycling">
            <i:Interaction.Behaviors>
                <local:NodeTreeSelectionBehavior SelectedItem="{Binding MySelectedItem}" />
            </i:Interaction.Behaviors>
        </TreeView>
    <Grid>
<UserControl>

Solution 2

I solved this problem by creating custom controls for TreeView, TreeViewItem and VirtualizingStackPanel. A part of the solution is from http://code.msdn.microsoft.com/Changing-selection-in-a-6a6242c8.

Each TreeItem (bound item) requires to know its parent (enforced by ITreeItem).

public interface ITreeItem {
    ITreeItem Parent { get; }
    IList<ITreeItem> Children { get; }
    bool IsSelected { get; set; }
    bool IsExpanded { get; set; }
}

When IsSelected is set on any TreeItem the view model is notified and raises an event. The corresponding event listener in the view calls BringItemIntoView on the TreeView.

The TreeView finds all TreeViewItems on the path to the selected item and brings them into view.

And here the rest of the code:

public class SelectableVirtualizingTreeView : TreeView {
    public SelectableVirtualizingTreeView() {
        VirtualizingStackPanel.SetIsVirtualizing(this, true);
        VirtualizingStackPanel.SetVirtualizationMode(this, VirtualizationMode.Recycling);
        var panelfactory = new FrameworkElementFactory(typeof(SelectableVirtualizingStackPanel));
        panelfactory.SetValue(Panel.IsItemsHostProperty, true);
        var template = new ItemsPanelTemplate { VisualTree = panelfactory };
        ItemsPanel = template;
    }

    public void BringItemIntoView(ITreeItem treeItemViewModel) {
        if (treeItemViewModel == null) {
            return;
        }
        var stack = new Stack<ITreeItem>();
        stack.Push(treeItemViewModel);
        while (treeItemViewModel.Parent != null) {
            stack.Push(treeItemViewModel.Parent);
            treeItemViewModel = treeItemViewModel.Parent;
        }
        ItemsControl containerControl = this;
        while (stack.Count > 0) {
            var viewModel = stack.Pop();
            var treeViewItem = containerControl.ItemContainerGenerator.ContainerFromItem(viewModel);
            var virtualizingPanel = FindVisualChild<SelectableVirtualizingStackPanel>(containerControl);
            if (virtualizingPanel != null) {
                var index = viewModel.Parent != null ? viewModel.Parent.Children.IndexOf(viewModel) : Items.IndexOf(treeViewItem);
                virtualizingPanel.BringIntoView(index);
                Focus();
            }
            containerControl = (ItemsControl)treeViewItem;
        }
    }

    protected override DependencyObject GetContainerForItemOverride() {
        return new SelectableVirtualizingTreeViewItem();
    }

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item) {
        base.PrepareContainerForItemOverride(element, item);
        ((TreeViewItem)element).IsExpanded = true;
    }

    private static T FindVisualChild<T>(Visual visual) where T : Visual {
        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++) {
            var child = (Visual)VisualTreeHelper.GetChild(visual, i);
            if (child == null) {
                continue;
            }
            var correctlyTyped = child as T;
            if (correctlyTyped != null) {
                return correctlyTyped;
            }
            var descendent = FindVisualChild<T>(child);
            if (descendent != null) {
                return descendent;
            }
        }
        return null;
    }
}

public class SelectableVirtualizingTreeViewItem : TreeViewItem {
    public SelectableVirtualizingTreeViewItem() {
        var panelfactory = new FrameworkElementFactory(typeof(SelectableVirtualizingStackPanel));
        panelfactory.SetValue(Panel.IsItemsHostProperty, true);
        var template = new ItemsPanelTemplate { VisualTree = panelfactory };
        ItemsPanel = template;
        SetBinding(IsSelectedProperty, new Binding("IsSelected"));
        SetBinding(IsExpandedProperty, new Binding("IsExpanded"));
    }

    protected override DependencyObject GetContainerForItemOverride() {
        return new SelectableVirtualizingTreeViewItem();
    }

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item) {
        base.PrepareContainerForItemOverride(element, item);
        ((TreeViewItem)element).IsExpanded = true;
    }
}

public class SelectableVirtualizingStackPanel : VirtualizingStackPanel {
    public void BringIntoView(int index) {
        if (index < 0) {
            return;
        }
        BringIndexIntoView(index);
    }
}

public abstract class TreeItemBase : ITreeItem {
    protected TreeItemBase() {
        Children = new ObservableCollection<ITreeItem>();
    }

    public ITreeItem Parent { get; protected set; }

    public IList<ITreeItem> Children { get; protected set; }

    public abstract bool IsSelected { get; set; }

    public abstract bool IsExpanded { get; set; }

    public event EventHandler DescendantSelected;

    protected void RaiseDescendantSelected(TreeItemViewModel newItem) {
        if (Parent != null) {
            ((TreeItemViewModel)Parent).RaiseDescendantSelected(newItem);
        } else {
            var handler = DescendantSelected;
            if (handler != null) {
                handler.Invoke(newItem, EventArgs.Empty);
            }
        }
    }
}

public class MainViewModel : INotifyPropertyChanged {
    private TreeItemViewModel _selectedItem;

    public MainViewModel() {
        TreeItemViewModels = new List<TreeItemViewModel> { new TreeItemViewModel { Name = "Item" } };
        for (var i = 0; i < 30; i++) {
            TreeItemViewModels[0].AddChildInitial();
        }
        TreeItemViewModels[0].IsSelected = true;
        TreeItemViewModels[0].DescendantSelected += OnDescendantSelected;
    }

    public event EventHandler DescendantSelected;

    public event PropertyChangedEventHandler PropertyChanged;

    public List<TreeItemViewModel> TreeItemViewModels { get; private set; }

    public TreeItemViewModel SelectedItem {
        get {
            return _selectedItem;
        }
        set {
            if (_selectedItem == value) {
                return;
            }
            _selectedItem = value;
            var handler = PropertyChanged;
            if (handler != null) {
                handler.Invoke(this, new PropertyChangedEventArgs("SelectedItem"));
            }
        }
    }

    private void OnDescendantSelected(object sender, EventArgs eventArgs) {
        var handler = DescendantSelected;
        if (handler != null) {
            handler.Invoke(sender, eventArgs);
        }
    }
}

public partial class MainWindow {
    public MainWindow() {
        InitializeComponent();
        var mainViewModel = (MainViewModel)DataContext;
        mainViewModel.DescendantSelected += OnMainViewModelDescendantSelected;
    }

    private void OnAddButtonClick(object sender, RoutedEventArgs e) {
        var mainViewModel = (MainViewModel)DataContext;
        var treeItemViewModel = mainViewModel.SelectedItem;
        if (treeItemViewModel != null) {
            treeItemViewModel.AddChild();
        }
    }

    private void OnMainViewModelDescendantSelected(object sender, EventArgs eventArgs) {
        _treeView.BringItemIntoView(sender as TreeItemViewModel);
    }

    private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {
        if (e.OldValue == e.NewValue) {
            return;
        }
        var treeView = (TreeView)sender;
        var treeItemviewModel = treeView.SelectedItem as TreeItemViewModel;
        var mainViewModel = (MainViewModel)DataContext;
        mainViewModel.SelectedItem = treeItemviewModel;
    }
}

And in XAML:

<controls:SelectableVirtualizingTreeView x:Name="_treeView" ItemsSource="{Binding TreeItemViewModels}" Margin="8" 
        SelectedItemChanged="OnTreeViewSelectedItemChanged">
    <controls:SelectableVirtualizingTreeView.ItemTemplate>
        <HierarchicalDataTemplate ... />
    </controls:SelectableVirtualizingTreeView.ItemTemplate>
</controls:SelectableVirtualizingTreeView>

Solution 3

If you used this (https://stackoverflow.com/a/9206992/8559138) decision and sometimes get InvalidOperationException, you can use my fixed decision:

I update currentParent layout if newParent is null and trying get ContainerFromIndex again.

 newParent = currentParent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
 if (newParent == null)
 {
      currentParent.UpdateLayout();
      virtualizingPanel.BringIndexIntoViewPublic(index);
      newParent = currentParent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
 }

Full decision:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

public class NodeTreeSelectionBehavior : Behavior<TreeView>
{
    public INode SelectedItem
    {
        get { return (INode)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(Node), typeof(NodeTreeSelectionBehavior),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemChanged));

    private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var newNode = e.NewValue as INode;
        if (newNode == null) return;
        var behavior = (NodeTreeSelectionBehavior)d;
        var tree = behavior.AssociatedObject;

        var nodeDynasty = new List<INode> { newNode };
        var parent = newNode.Parent;
        while (parent != null)
        {
            nodeDynasty.Insert(0, parent);
            parent = parent.Parent;
        }

        var currentParent = tree as ItemsControl;
        foreach (var node in nodeDynasty)
        {
            // first try the easy way
            var newParent = currentParent.ItemContainerGenerator.ContainerFromItem(node) as TreeViewItem;
            var index = 0;
            VirtualizingPanel virtualizingPanel = null;
            if (newParent == null)
            {
                // if this failed, it's probably because of virtualization, and we will have to do it the hard way.
                // this code is influenced by TreeViewItem.ExpandRecursive decompiled code, and the MSDN sample at http://code.msdn.microsoft.com/Changing-selection-in-a-6a6242c8/sourcecode?fileId=18862&pathId=753647475
                // see also the question at http://stackoverflow.com/q/183636/46635
                currentParent.ApplyTemplate();
                var itemsPresenter = (ItemsPresenter)currentParent.Template.FindName("ItemsHost", currentParent);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    currentParent.UpdateLayout();
                }

                virtualizingPanel = GetItemsHost(currentParent) as VirtualizingPanel;
                CallEnsureGenerator(virtualizingPanel);
                index = currentParent.Items.IndexOf(node);
                if (index < 0)
                {
                    throw new InvalidOperationException("Node '" + node + "' cannot be fount in container");
                }
                if (virtualizingPanel != null)
                {
                    virtualizingPanel.BringIndexIntoViewPublic(index);
                }
                newParent = currentParent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
                if (newParent == null)
                {
                    currentParent.UpdateLayout();
                    virtualizingPanel.BringIndexIntoViewPublic(index);
                    newParent = currentParent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
                }
            }

            if (newParent == null)
            {
                  throw new InvalidOperationException("Tree view item cannot be found or created for node '" + node + "'");
            }

            if (node == newNode)
            {
                newParent.IsSelected = true;
                newParent.BringIntoView();
                break;
            }

            newParent.IsExpanded = true;
            currentParent = newParent;
        }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
    }

    private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        SelectedItem = e.NewValue as INode;
    }

    #region Functions to get internal members using reflection

    // Some functionality we need is hidden in internal members, so we use reflection to get them

    #region ItemsControl.ItemsHost

    static readonly PropertyInfo ItemsHostPropertyInfo = typeof(ItemsControl).GetProperty("ItemsHost", BindingFlags.Instance | BindingFlags.NonPublic);

    private static Panel GetItemsHost(ItemsControl itemsControl)
    {
        Debug.Assert(itemsControl != null);
        return ItemsHostPropertyInfo.GetValue(itemsControl, null) as Panel;
    }

    #endregion ItemsControl.ItemsHost

    #region Panel.EnsureGenerator

    private static readonly MethodInfo EnsureGeneratorMethodInfo = typeof(Panel).GetMethod("EnsureGenerator", BindingFlags.Instance | BindingFlags.NonPublic);

    private static void CallEnsureGenerator(Panel panel)
    {
        Debug.Assert(panel != null);
        EnsureGeneratorMethodInfo.Invoke(panel, null);
    }

    #endregion Panel.EnsureGenerator

    #endregion Functions to get internal members using reflection
}

And XAML:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:local="clr-namespace:MyProject">
<Grid>
    <TreeView ItemsSource="{Binding MyItems}"
              ScrollViewer.CanContentScroll="True"
              VirtualizingStackPanel.IsVirtualizing="True"
              VirtualizingStackPanel.VirtualizationMode="Recycling">
        <i:Interaction.Behaviors>
            <local:NodeTreeSelectionBehavior SelectedItem="{Binding MySelectedItem}" />
        </i:Interaction.Behaviors>
    </TreeView>
<Grid>

Solution 4

I used an attached property to solve this issue.

public class TreeViewItemBehaviour
{
    #region IsBroughtIntoViewWhenSelected

    public static bool GetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem)
    {
        return (bool)treeViewItem.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetIsBroughtIntoViewWhenSelected(
      TreeViewItem treeViewItem, bool value)
    {
        treeViewItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "IsBroughtIntoViewWhenSelected",
        typeof(bool),
        typeof(TreeViewItemBehaviour),
        new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    static void OnIsBroughtIntoViewWhenSelectedChanged(
      DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TreeViewItem item = depObj as TreeViewItem;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
        {
            item.Loaded += item_Loaded;
        }
        else
        {
            item.Loaded -= item_Loaded;
        }
    }

    static void item_Loaded(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;
        if (item != null)
            item.BringIntoView();
    }

    #endregion // IsBroughtIntoViewWhenSelected

}

And in my XAML style for a TreeViewItem, I just set the property to true

<Setter Property="Behaviours:TreeViewItemBehaviour.IsBroughtIntoViewWhenSelected" Value="True" />

HTH

Share:
13,020

Related videos on Youtube

pousi
Author by

pousi

Updated on April 16, 2022

Comments

  • pousi
    pousi almost 2 years

    Is there a way to select manually a node in virtualizing TreeView and then bring it into view?

    The data model I'm using with my TreeView is implemented based on the VM-M-V model. Each TreeViewItem's IsSelected property is binded to a corresponing property in ViewModel. I've also created a listener for TreeView's ItemSelected event where I call BringIntoView() for the selected TreeViewItem.

    The problem with this approach seems to be that the ItemSelected event won't be raised until the actual TreeViewItem is created. So with the virtualization enabled node selection won't do anything until the TreeView is scrolled enough and then it jumps "magically" to the selected node when the event is finally raised.

    I'd really like to use virtualization because I have thousands of nodes in my tree and I've already seen quite impressive performance improvements when the virtualization has been enabled.

    • akjoshi
      akjoshi over 13 years
      Hi, I am also stuck with the same issue. You got any solution for this problem?
  • springy76
    springy76 over 12 years
    When you use a virtualized TreeView then the TreeViewItem you wish to select most likely isn't created yet -- so it's absolutely useless to apply any Style for TreeViewItem.
  • Cameron Peters
    Cameron Peters over 10 years
    +1: This is working well for me in Windows 7 / dotNet 4.0. Does anyone know if the methods called through reflection are available in dotNet 4.5 or 4.5.1. (I will check myself but wanted to provide an opportunity for another developer to share their wisdom :)
  • splintor
    splintor almost 10 years
    @CameronPeters .Net 4.5 added BringIndexIntoViewPublic (msdn.microsoft.com/en-us/library/…), so if you are using .Net 4.5 or later, you can call it directly instead of getting it via reflection.
  • user3313608
    user3313608 about 7 years
    You guys are awesome - I manged to implement this solution - it does work in Windows 10 and it also works with the BringIndexIntoViewPublic method in .Net 4.5 - cool :-) I used an interface as a reference instead of the actual node class - so, I guess the pattern is not completely broken. I wonder if there is a replacement for the other method: EnsureGenerator and Property: ItemsHost in 4.5 or later? How come you have to use hidden items to do something so common?
  • Majed DH
    Majed DH over 5 years
    Worked like a charm> You're a life saver, I've been searching for 16 hours about a solution ! I wish I can give you more than one plus!
  • Majed DH
    Majed DH over 5 years
    Sometimes InvalidOperationException is thrown, Any Idea ?? @splintor
  • splintor
    splintor over 5 years
    Sorry, but I no longer work with C#, so I can't help you.
  • g t
    g t almost 5 years
    Additionally, ItemsHost is available via IHierarchicalVirtualizationAndScrollInfo interface which TreeViewItem implements. EnsureGenerator can also be replaced with a call to virtualizingPanel.Children (which calls it), although this obscures the intent somewhat and could change between versions.