Click here to Skip to main content
15,886,362 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 289.1K   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;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Text;


namespace QuickZip.MiniHtml2
{
    /// <summary>
    /// An Item entry, can store a string and an object (Pie)
    /// </summary>
    public class PropertyItemType 
    {
    	public string key;
        public string value;
        public object attachment;
        public PropertyItemType(string aKey, string aValue)
        {
        	key = aKey;
            value = aValue;
        }
        public PropertyItemType(string aKey, object anAttachment)
        {   
        	key = aKey;
            attachment = anAttachment;
        }
        public PropertyItemType(string aKey, string aValue, object anAttachment)
        {
        	key = aKey;
            value = aValue;
            attachment = anAttachment;
        }
    }
    /// <summary>
    /// Variable List that use "Key" to store PropertyItemType
    /// </summary>
    public class PropertyList : ListDictionary
    {
        public bool createIfNotExist = true;

        /// <summary>
        /// Get an item from the list
        /// </summary>
        private PropertyItemType getPropertyInfo(string aKey)
        {
            if (Contains(aKey))
            {
                foreach (DictionaryEntry de in this)
                {
                    if ((string)de.Key == aKey)
                    {
                        return (PropertyItemType)de.Value;
                    }
                }
            }

            PropertyItemType retVal = new PropertyItemType(aKey, "");
            if (createIfNotExist) { Add(aKey, retVal); }
            return retVal;
        }
        /// <summary>
        /// Get an item from the list
        /// </summary>
        private PropertyItemType getPropertyInfo(Int32 anId)
        {            
            IDictionaryEnumerator Enum = GetEnumerator();
            if (Count >= anId)
            {
                for (int i = 0; i <= anId; i++) { Enum.MoveNext(); }
                return (PropertyItemType)Enum.Value;
            }
            return new PropertyItemType(anId.ToString(),"");
        }
        /// <summary>
        /// Set or Add an item to the list
        /// </summary>
        private void setPropertyInfo(string aKey, PropertyItemType aValue)
        {
            VerifyType(aValue);
            if (Contains(aKey))
            {
                this.Remove(aKey);
            }
            Add(aKey, aValue);
        }
        /// <summary>
        /// Check item before add.
        /// </summary>        
        private void VerifyType(object value)
        {
            if (!(value is PropertyItemType))
            { throw new ArgumentException("Invalid Type."); }
        }
        /// <summary>
        /// Add a new PropertyInfo
        /// </summary>
        public void Add(string aKey, string aValue)
        {
            Add(aKey, new PropertyItemType(aKey, aValue));
        }
        /// <summary>
        /// Add a new PropertyInfo
        /// </summary>
        public void Add(string aKey, string aValue, object anAttachment)
        {
            Add(aKey, new PropertyItemType(aKey, aValue,anAttachment));
        }
        /// <summary>
        /// Retrieve a PropertyItem using a key
        /// </summary>
        public PropertyItemType this[String aKey]
        {
            get
            {
                return getPropertyInfo(aKey);
            }
            set
            {
                setPropertyInfo(aKey, value);
            }
        }
        /// <summary>
        /// Retrieve a PropertyItem using an id
        /// </summary>
        public PropertyItemType this[Int32 anId]
        {
            get
            {
                return getPropertyInfo(anId);
            }

        }
        
        public string Html()
        {
        	string retVal = "";
        	for (Int32 i = 0; i < this.Count; i++)
        	{
        		PropertyItemType item = this[i];
        		retVal += " " + item.key + "=\"" + item.value + "\"";
        	}
        		
        	return retVal;
        }
        
        
        public PropertyList Clone()
        {
        	PropertyList retVal = new PropertyList();
        	foreach (PropertyItemType item in this)
        		retVal.Add(item.key, item.value);
        	return retVal;
        }
        
		public override string ToString()
		{
			string retVal = "";
			for (int i = 0; i < Count; i++)
				retVal += String.Format(" {0}=\"{1}\"; ", this[i].key, this[i].value);
			if (retVal == "")
				return " ";
			else return retVal;
		}
        
    
        public static void DebugUnit()
        {
            PropertyList list = new PropertyList();
            list["abc"].value = "abcd";
            list["bcd"].value = "bcde";
            list["cde"].value = "cdef";
            list.Remove("abc");

            Debug.Assert((list["bcd"].value == "bcde"), "PropertyList Failed.");
            Debug.Assert((list["abc"].value == ""), "PropertyList Failed.");
        }

        public static PropertyList FromString(string s)
        {
            return Utils.ExtravtVariables(s);
        }

        

    }
}

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