Click here to Skip to main content
15,896,474 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.7K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using MediaAssistant.Controls.WaitScreen;
using MefBasic;
using MefBasic.Threading;
using MediaAssistant.DAL;

namespace MediaAssistant.Controls.MovieThumbnails
{
    [Export]
    public partial class MovieThumbnailsView : IView
    {
        public MovieThumbnailsView()
        {
            InitializeComponent();
        }

        private void HandleScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            ShowVisibleItems(sender);
        }

        private void ShowVisibleItems(object sender)
        {
            var scrollViewer = (FrameworkElement) sender;
            var visibleAreaEntered = false;
            var visibleAreaLeft = false;
            var invisibleItemDisplayed = 0;
            foreach (Movie item in thumbnailListBox.Items)
            {
                if (item.IsVisible) continue;
                var listBoxItem = (FrameworkElement) thumbnailListBox.ItemContainerGenerator.ContainerFromItem(item);
                if (visibleAreaLeft==false && IsFullyOrPartiallyVisible(listBoxItem, scrollViewer))
                {
                    visibleAreaEntered = true;
                }
                else if (visibleAreaEntered)
                {
                    visibleAreaLeft = true;
                }
                if(visibleAreaEntered)
                {
                    if(visibleAreaLeft && ++invisibleItemDisplayed>10)
                        break;
                    var job = new Job(MakeVisible);
                    job.Store.Add(item);
                    job.Start();
                }
            }
        }

        private static void MakeVisible(Job obj)
        {
            obj.Store.GetObject<Movie>().IsVisible = true;
        }

        protected bool IsFullyOrPartiallyVisible(FrameworkElement child, FrameworkElement scrollViewer)
        {
            var childTransform = child.TransformToAncestor(scrollViewer);
            var childRectangle = childTransform.TransformBounds(new Rect(new Point(0, 0), child.RenderSize));
            var ownerRectangle = new Rect(new Point(0, 0), scrollViewer.RenderSize);
            return ownerRectangle.IntersectsWith(childRectangle);
        }

    }
}

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