Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

ItemContainerGenerator Recycling Problem

$
0
0

Hi,

I am trying to use Recycling within my custom virtualizing panel.

Here is my code:

protected override Size MeasureOverride(Size constraint)
{

    ... // computatons of firstVisibleItemIndex
    IItemContainerGenerator generator = this.ItemContainerGenerator;
    

    double h = 0.0;

    dboule w = 0.0;


    GeneratorPosition startPos = generator.GeneratorPositionFromIndex(firstVisibleItemIndex);

    using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
    {
        for (int itemIndex = firstVisibleItemIndex; itemIndex <= lastVisibleItemIndex; itemIndex++)
        {
            bool newlyRealized;
            UIElement child = generator.GenerateNext(out newlyRealized) as UIElement;
            if (newlyRealized)
            {
                generator.PrepareItemContainer(child);
            }

            if (childIndex >= children.Count)
            {
                base.AddInternalChild(child);
            }
            else
            {
                base.InsertInternalChild(itemIndex - firstVisibleItemIndex, child);
            }

            child.Measure(new Size(itemWidth, itemHeight));

            w = Math.Max(child.DesireSize.Width, w);

            h += child.DesireSize.Height;

            if(h >= constraint.Height)

           {

            lastVisibleItemInde = itemIndex;


            break;

           }


        }
    }

    CleanUpItems(firstVisibleIndex, lastVisibleIndex);
 
    return new Size(w, h);
}

The CleanUp method looks like this:

private void CleanUpItems(int firstVisibleItemIndex, int lastVisibleItemIndex)
{
    UIElementCollection children = this.InternalChildren;
    IItemContainerGenerator generator = this.ItemContainerGenerator;
    for (int i = children.Count-1; i >= 0; i--)
    {
        GeneratorPosition childGeneratorPos = new GeneratorPosition(i, 0);
        int itemIndex = generator.IndexFromGeneratorPosition(childGeneratorPos);
        if (itemIndex < firstVisibleItemIndex || itemIndex > lastVisibleItemIndex)
        {
            generator.Recycle(childGeneratorPos, 1);
            RemoveInternalChildRange(i, 1);
        }
    }
}

As you can see I run measureoverride and figure out the visible items based on their sum of heights.

When I start the application it shows me the first 20 items but when I start scrolling I get error that visual already has parent.

What can I do to solve this with recycling? Any ideas? 


Viewing all articles
Browse latest Browse all 18858

Trending Articles