Click here to Skip to main content
15,891,423 members
Articles / Desktop Programming / WPF

Wrap Panel Virtualization

Rate me:
Please Sign up or sign in to vote.
4.95/5 (18 votes)
2 Jan 2012CPOL2 min read 53.5K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using MediaAssistant.DAL.Constants;

namespace MediaAssistant.DAL.Virtualization
{
    public class LibraryItemProvider : IItemsProvider<LibraryItem>
    {
        private int _count;
        private readonly Dictionary<int, LibraryItem> _startIndexDictionary;
        private readonly IEnumerable<LibraryItem> _rootItems;

        public LibraryItemProvider(IEnumerable<LibraryItem> rootItems)
        {
            _rootItems = rootItems;
            _startIndexDictionary = new Dictionary<int, LibraryItem>();
        }

        private void CalculateCount()
        {
            _count = 0;
            foreach (var rootItem in _rootItems)
            {
                _count++;
                rootItem.Lavel = 0;
                _startIndexDictionary.Add(_count - 1, rootItem);
                GetChildrenCount(rootItem, 0);
            }
        }

        private void GetChildrenCount(LibraryItem item, int label)
        {
            if (!item.IsExpanded)
            {
                return;
            }
            if (LibraryItemType.CanHaveChildren(item.Type) == false)
            {
                return;
            }
            if (LibraryItemType.IsLastParent(item.Type) == false)
            {
                foreach (var child in item.OrderedChildren)
                {
                    _count++;
                    child.Lavel = label + 1;
                    _startIndexDictionary.Add(_count - 1, child);
                    GetChildrenCount(child, label + 1);
                }
            }
            else
            {
                _count += item.Children.Count;
            }
        }

        public int FetchCount()
        {
            lock (DatabaseManager.LockObject)
            {
                CalculateCount();
            }
            return _count;
        }

        public IList<LibraryItem> FetchRange(int startIndex, int count)
        {
            lock (DatabaseManager.LockObject)
            {
                var skippedItems = 0;
                var items = new List<LibraryItem>();
                foreach (var kv in _startIndexDictionary.OrderBy(kv => kv.Key))
                {
                    var endeIndex = kv.Key;
                    if (kv.Value.IsExpanded)
                    {
                        endeIndex += kv.Value.Children.Count;
                    }
                    if (endeIndex < startIndex)
                    {
                        skippedItems = endeIndex + 1;
                        continue;
                    }
                    if (skippedItems < startIndex)
                    {
                        skippedItems++;
                    }
                    else
                    {
                        items.Add(kv.Value);
                        if (items.Count == count)
                            return items;
                    }

                    if (kv.Value.IsExpanded && LibraryItemType.IsLastParent(kv.Value.Type))
                    {
                        foreach (var item in kv.Value.OrderedChildren)
                        {
                            if (skippedItems < startIndex)
                            {
                                skippedItems++;
                            }
                            else
                            {
                                item.Lavel = kv.Value.Lavel + 1;
                                items.Add(item);
                                if (items.Count == count)
                                    return items;
                            }
                        }
                    }
                }
                return items;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions