// find the top-level ItemsControl (in a presentation of hierarchical data)
        // for the given mid-level container.
        // NOTE: This assumes that the only container types are TreeViewItem and
        // GroupItem.   This is true in 4.5.  If hierarchical virtualization
        // is ever extended to other container types, this method will need to change.
        private ItemsControl GetScrollingItemsControl(UIElement container)
        {
            if (container is TreeViewItem)
            {
                ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(container);
                while (parent != null)
                {
                    TreeView tv = parent as TreeView;
                    if (tv != null)
                    {
                        return tv;
                    }

                    parent = ItemsControl.ItemsControlFromItemContainer(parent);
                }
            }
            else if (container is GroupItem)
            {
                DependencyObject parent = container;
                do
                {
                    parent = VisualTreeHelper.GetParent(parent);
                    ItemsControl parentItemsControl = parent as ItemsControl;
                    if (parentItemsControl != null)
                    {
                        return parentItemsControl;
                    }
                } while (parent != null);
            }
            else
            {
                string name = (container == null) ? "null" : container.GetType().Name;
                Debug.Assert(false, "Unexpected container type: " + name);
            }

            return null;
        }