Click here to Skip to main content
15,885,914 members
Articles / Web Development / HTML

WPF x FileExplorer x MVVM

Rate me:
Please Sign up or sign in to vote.
4.99/5 (52 votes)
24 Nov 2012LGPL323 min read 289K   9.4K   228  
This article describe how to construct FileExplorer controls included DirectoryTree and FileList, using Model-View-ViewModel (MVVM) pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinch;
using System.ComponentModel;
using System.Windows;
using QuickZip.UserControls.Logic.Tools.DragnDrop;

namespace QuickZip.UserControls.MVVM.Model
{
    [Flags]
    public enum AddActions : int
    {
        None = 1 << 0,
        Copy = 1 << 1,
        Link = 1 << 2,
        Move = 1 << 3, 
        All = AddActions.Copy | AddActions.Link | AddActions.Move
    }

    public abstract class DirectoryModel<FI, DI, FSI> : EntryModel<FI, DI, FSI>
        where DI : FSI
        where FI : FSI
    {        
        protected enum DirPropertyEnum { isChildReadOnly }

        #region Constructor

        public DirectoryModel(DI embeddedDir)
            : base(embeddedDir)
        {

        }

        #endregion
        

        #region Methods

        protected abstract IEnumerable<EntryModel<FI, DI, FSI>> getSubEntries(string filter = "*", bool directory = true, bool file = true);        
        public abstract void Delete(FSI[] entries);        
        
        public IEnumerable<EntryModel<FI, DI, FSI>> GetSubEntries(bool forceReload = false, string filter = "*", bool directory = true, bool file = true)
        {
            if (!forceReload && _subEntriesList != null)
            {
                List<EntryModel<FI, DI, FSI>> _subEntriesListClone = new List<EntryModel<FI, DI, FSI>>();
                lock (_subEntriesList)
                    _subEntriesListClone.AddRange(_subEntriesList);

                foreach (EntryModel<FI, DI, FSI> subEntry in _subEntriesListClone)
                       if ((subEntry is FileModel<FI, DI, FSI> && file) ||
                        subEntry is DirectoryModel<FI, DI, FSI> && directory)
                            yield return subEntry;
                yield break;
            }
            else
            {
                List<EntryModel<FI, DI, FSI>> retList = new List<EntryModel<FI, DI, FSI>>();
                IEnumerator<EntryModel<FI, DI, FSI>> enumerator = getSubEntries(filter, true, true).GetEnumerator();
                while (enumerator.MoveNext())
                {
                    if ((enumerator.Current is FileModel<FI, DI, FSI> && file) || 
                        enumerator.Current is DirectoryModel<FI, DI, FSI> && directory)
                        yield return enumerator.Current;
                    retList.Add(enumerator.Current);
                }

                _subEntriesList = retList;
                yield break;
            }
        }

        //protected void InvalidateChild()
        //{
        //    OnChildRefreshNeeded(this, new EventArgs());
        //}

        public abstract bool HasChild(FSI entry);

        public bool EqualsOrHasChild(EntryModel<FI, DI, FSI> entryModel)
        {
            if (entryModel == null) return false;
            return this.Equals(entryModel) || HasChild(entryModel.EmbeddedEntry);
        }

        public override ViewModel.EntryViewModel<FI, DI, FSI> ToViewModel(Profile<FI, DI, FSI> profile)
        {
            return new ViewModel.DirectoryViewModel<FI, DI, FSI>(profile, this) { };
        }

        public ViewModel.DirectoryViewModel<FI, DI, FSI> ToDirectoryViewModel(Profile<FI, DI, FSI> profile, ViewModel.DirectoryViewModel<FI, DI, FSI> parentViewModel = null)
        {
            return new ViewModel.DirectoryViewModel<FI, DI, FSI>(profile, this) { ParentViewModel = parentViewModel };
        }      

        #endregion

        #region Data

        private List<EntryModel<FI, DI, FSI>> _subEntriesList;
        private bool _hasSubDirectories = true;
        private bool _isSupportAdd = false;
        private AddActions _supportedAddAction = AddActions.Copy;
        private bool _isLink = false;

        #endregion

        #region Public Properties

        /// <summary>
        /// If IsLink, DirectoryTree will not list it's subdirectory.
        /// </summary>
        public bool IsLink { get { return _isLink; } protected set { _isLink = value; NotifyPropertyChanged("IsLink"); } }
        public DI EmbeddedDirectory { get { return (DI)EmbeddedEntry; } }
        public bool IsSupportAdd { get { return _isSupportAdd; } protected set { _isSupportAdd = value; } }
        
        internal bool IsCached { get { return _subEntriesList != null; } }

        #region HasSubDirectories
        static PropertyChangedEventArgs hasSubDirectoriesChangeArgs =
            ObservableHelper.CreateArgs<DirectoryModel<FI, DI, FSI>>(x => x.HasSubDirectories);
        /// <summary>
        /// If the current directory has sub directories. (default true)
        /// </summary>
        public bool HasSubDirectories
        {
            get { return _hasSubDirectories; }
            protected set { _hasSubDirectories = value; NotifyPropertyChanged(hasSubDirectoriesChangeArgs); }
        }
        #endregion

        #endregion
    }

}

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 GNU Lesser General Public License (LGPLv3)


Written By
Founder
Hong Kong Hong Kong

Comments and Discussions