Click here to Skip to main content
15,892,517 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 290.4K   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;

namespace QuickZip.IO.PIDL.UserControls.ViewModel
{
    public abstract class HierarchyViewModel : ExViewModel
    {

        #region Constructor

        internal HierarchyViewModel()
            : base()
        {

        }

        public HierarchyViewModel(RootModelBase rootModel, Model.ExModel model)
            : base(rootModel, model)
        {
            _parent = null;
        }

        public HierarchyViewModel(Model.ExModel model) : base(model)             
        {
            _parent = null;
        }

        public HierarchyViewModel(RootModelBase rootModel, HierarchyViewModel parentModel, Model.ExModel model)
            : base(rootModel, model)
        {
            _parent = parentModel;
        }

        #endregion

        #region Methods        

        protected virtual void OnExpanded()
        {

        }

        protected virtual void OnCollapsed()
        {

        }

        public void CollapseTree()
        {
            this.IsExpanded = false;
            if (Parent != null)
                Parent.CollapseTree();
        }

        #endregion

        #region Data
        private HierarchyViewModel _parent;
        private bool _isExpanded = false;
        private bool _isSelected = false;
        #endregion

        #region Properties

        //http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    NotifyPropertyChanged("IsExpanded");
                    if (value)
                        OnExpanded();
                    else OnCollapsed();
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;
            }
        }

        public virtual void OnSelected()
        {

        }

        public virtual void OnUnselected()
        {

        }


        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    if (value && this.Parent != null)
                        this.Parent.IsExpanded = true;
                    NotifyPropertyChanged("IsSelected");
                    if (value)
                        OnSelected();
                    else OnUnselected();
                }
            }
        }

        public HierarchyViewModel Parent
        {
            get { return _parent; }
            protected set { _parent = value; NotifyPropertyChanged("Parent"); }
        }

        #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