Click here to Skip to main content
15,896,063 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;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Windows;
using MediaAssistant.Controls.Dialog;
using MediaAssistant.Data;
using MefBasic;
using MefBasic.Commans;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Constants;

namespace MediaAssistant.Controls.AlternativeLocation
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AlternativeLocationPresenter : APresenter<AlternativeLocationView>, IDialogContentPresenter
    {
        [ImportingConstructor]
        public AlternativeLocationPresenter(AlternativeLocationView view)
            : base(view)
        {
            SelectedMovieChangedCommand = new DelegateCommand(ExecuteSelectedMovieChanged);
            MakeDefaultLocationCommand = new DelegateCommand(ExecuteMakeDefaultLocation, CanExecuteMakeDefaultLocation);
        }

        private bool CanExecuteMakeDefaultLocation(object arg)
        {
            return SelectedMovie != null;
        }

        public DelegateCommand MakeDefaultLocationCommand { get; set; }

        private void ExecuteMakeDefaultLocation(object obj)
        {
            var oldDefaultLocation = Movie.FullPath;
            DatabaseManager.Instance.MakeDefaultLocation(Movie, SelectedMovie.FullPath);
            SelectedMovie.FullPath = oldDefaultLocation;
            var fileInfo = new FileInfo(oldDefaultLocation);
            SelectedMovie.FileName = fileInfo.Name;
            SelectedMovie.FullPath = oldDefaultLocation;
            SelectedMovie.Size = fileInfo.Length;
            SelectedMovie.SizeText = Utility.GetFileSize(fileInfo.Length);
        }

        public DelegateCommand SelectedMovieChangedCommand { get; set; }

        private void ExecuteSelectedMovieChanged(object obj)
        {
            SelectedMovie = (Movie) obj;
            MakeDefaultLocationCommand.RaiseCanExecuteChanged();
        }

        public bool OnClosing(DialogResult dialogResult)
        {
            return true;
        }



        public Movie SelectedMovie
        {
            get { return (Movie)GetValue(SelectedMovieProperty); }
            set { SetValue(SelectedMovieProperty, value); }
        }

        public static readonly DependencyProperty SelectedMovieProperty =
            DependencyProperty.Register("SelectedMovie", typeof(Movie), typeof(AlternativeLocationPresenter), new UIPropertyMetadata(null));



        [Import]
        public LibraryDataSource DataSource
        {
            get { return (LibraryDataSource)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(LibraryDataSource), typeof(AlternativeLocationPresenter), new UIPropertyMetadata(null));




        public Movie Movie
        {
            get { return (Movie)GetValue(MovieProperty); }
            set { SetValue(MovieProperty, value); }
        }

        public static readonly DependencyProperty MovieProperty =
            DependencyProperty.Register("Movie", typeof(Movie), typeof(AlternativeLocationView), new UIPropertyMetadata(null));



        public ObservableCollection<Movie> AlternativeMovies
        {
            get { return (ObservableCollection<Movie>)GetValue(AlternativeMoviesProperty); }
            set { SetValue(AlternativeMoviesProperty, value); }
        }

        public static readonly DependencyProperty AlternativeMoviesProperty =
            DependencyProperty.Register("AlternativeMovies", typeof(ObservableCollection<Movie>), typeof(AlternativeLocationView), new UIPropertyMetadata(null));



        public void ShowAlternativeLocations(Movie movie)
        {
            Movie = movie;
            AlternativeMovies = new ObservableCollection<Movie>();
            var index = 1;
            foreach (var alternativeLocation in movie.AlternativeLocations)
            {
                if(File.Exists(alternativeLocation.Location)==false)
                {
                    continue;
                }
                var fileInfo = new FileInfo(alternativeLocation.Location);
                var alternativeMovie = new Movie
                {
                    FileName = fileInfo.Name,
                    FullPath = alternativeLocation.Location,
                    Size = fileInfo.Length,
                    SizeText = Utility.GetFileSize(fileInfo.Length),
                    CreateDate = DateTime.Now,
                    Status = MovieStatus.Pending,
                    Title = movie.Title,
                    PlayCount = 0,
                    Index = index++
                };
                AlternativeMovies.Add(alternativeMovie);
            }
            if (AlternativeMovies.Count==0)
                return;
            SelectedMovie = AlternativeMovies.First();
            if(Resolve<IDialogPresenter>().ShowDialog("Alternative Locations", this)==DialogResult.Ok)
            {
                foreach (var alternativeMovie in AlternativeMovies.Where(m=>string.IsNullOrEmpty(m.Title)==false))
                {
                    var fullPath = alternativeMovie.FullPath;
                    DatabaseManager.Instance.Delete(movie.AlternativeLocations.First(l => l.Location == fullPath));
                    DatabaseManager.Instance.AddMovie(alternativeMovie);
                }
                DatabaseManager.Instance.UpdateProcessingTitle(DataSource.ShowDrivesMoviesOnly);
            }
        }
    }
}

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