This project is read-only.
1
Vote

InvalidOperationException when using VWP as ItemsPanel in ListBox when reached the end of the list

description

Sorry for my english
to solve this problem(10000 items on panel) i made this:
    private int GetNextSectionClosestIndex(int itemIndex)
    {
        var abstractItem = _abstractPanel[itemIndex];
        if (abstractItem.Section < _abstractPanel.SectionCount - 1)
        {
            try//************************!!!!!!!!!!!!!!!!!!!**************
            {
                var ret = _abstractPanel.
                    Where(x => x.Section == abstractItem.Section + 1).
                    OrderBy(x => Math.Abs(x.SectionIndex - abstractItem.SectionIndex)).
                    First();
                return ret._index;
            }
            catch (NullReferenceException) { return ERROR_INDEX; }
            catch (InvalidOperationException) { return ERROR_INDEX; }
        }
        else
            return itemIndex;
    }


    private const int ERROR_INDEX = -1;

private void NavigateDown()
    {
        var gen = _generator.GetItemContainerGeneratorForPanel(this);
        UIElement selected = (UIElement)Keyboard.FocusedElement;
        int itemIndex = gen.IndexFromContainer(selected);
        int depth = 0;
        while (itemIndex == -1)
        {
            selected = (UIElement)VisualTreeHelper.GetParent(selected);
            itemIndex = gen.IndexFromContainer(selected);
            depth++;
        }
        DependencyObject next = null;
        if (Orientation == Orientation.Horizontal)
        {
            int nextIndex = GetNextSectionClosestIndex(itemIndex);

            if (nextIndex == ERROR_INDEX) return; //****************!!!!!!!!!!!!!!!!!!!!!!!!*******************

            next = gen.ContainerFromIndex(nextIndex);
            while (next == null)
            {
                SetVerticalOffset(VerticalOffset + 1);
                UpdateLayout();
                next = gen.ContainerFromIndex(nextIndex);
            }
        }
        else
        {
            if (itemIndex == _abstractPanel._itemCount - 1)
                return;
            next = gen.ContainerFromIndex(itemIndex + 1);
            while (next == null)
            {
                SetHorizontalOffset(HorizontalOffset + 1);
                UpdateLayout();
                next = gen.ContainerFromIndex(itemIndex + 1);
            }
        }
        while (depth != 0)
        {
            next = VisualTreeHelper.GetChild(next, 0);
            depth--;
        }
        (next as UIElement).Focus();
    }


    private void NavigateRight()
    {
        var gen = _generator.GetItemContainerGeneratorForPanel(this);
        UIElement selected = (UIElement)Keyboard.FocusedElement;
        int itemIndex = gen.IndexFromContainer(selected);
        int depth = 0;
        while (itemIndex == -1)
        {
            selected = (UIElement)VisualTreeHelper.GetParent(selected);
            itemIndex = gen.IndexFromContainer(selected);
            depth++;
        }
        DependencyObject next = null;
        if (Orientation == Orientation.Vertical)
        {
            int nextIndex = GetNextSectionClosestIndex(itemIndex);

            if (nextIndex == ERROR_INDEX) return;//*************************************!!!!!!*************************

            next = gen.ContainerFromIndex(nextIndex);
            while (next == null)
            {
                SetHorizontalOffset(HorizontalOffset + 1);
                UpdateLayout();
                next = gen.ContainerFromIndex(nextIndex);
            }
        }
        else
        {
            if (itemIndex == _abstractPanel._itemCount - 1)
                return;
            next = gen.ContainerFromIndex(itemIndex + 1);
            while (next == null)
            {
                SetVerticalOffset(VerticalOffset + 1);
                UpdateLayout();
                next = gen.ContainerFromIndex(itemIndex + 1);
            }
        }
        while (depth != 0)
        {
            next = VisualTreeHelper.GetChild(next, 0);
            depth--;
        }
        (next as UIElement).Focus();
    }

Thanx for you work!

comments