Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / C#

Silverlight Auto Complete Treeview

Rate me:
Please Sign up or sign in to vote.
4.70/5 (7 votes)
18 Jun 2009CPOL5 min read 42.7K   1.6K   18  
Silverlight auto-complete treeview which takes hierarchical data as its datasource.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;




namespace AutoCompleteTextBox
{
    public class HierarchicalCity : INotifyPropertyChanged
    {
        #region Constructors
        public HierarchicalCity(string city)
        {
            CityName = city;
            SubClass = new ObservableCollection<HierarchicalCity>();
        }
        public HierarchicalCity(string City, ObservableCollection<HierarchicalCity> sub)
        {
            CityName = City;
            SubClass = sub;
        }
        #endregion

        #region Data Properties
        private string _CityName = "";
        public string CityName
        {
            get { return _CityName; }
            set { _CityName = value; OnPropertyChanged("CityName"); }
        }

        private bool isExpanded;
        public bool IsExpanded
        {
            get { return isExpanded; }
            set
            {
                isExpanded = value;
                OnPropertyChanged("IsExpanded");
            }
        }


        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

        private ObservableCollection<HierarchicalCity> _SubClass = new ObservableCollection<HierarchicalCity>();
        public ObservableCollection<HierarchicalCity> SubClass
        {
            get { return _SubClass; }
            set { _SubClass = value; OnPropertyChanged("SubClass"); }
        }
        #endregion

        #region INotify Implementation
        public event PropertyChangedEventHandler PropertyChanged;


        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Expansion / Collapse Function
        public bool ExpandSuperclasses(HierarchicalCity itemToLookFor)
        {
            return ApplyActionToSuperclasses(itemToLookFor, superclass => superclass.IsExpanded = true);
        }

        public void CollapseAll()
        {
            ApplyActionToAllItems();//item => item.IsExpanded = false);
            //ApplyActionToAllItems(item => item.IsSelected = false);
        }

        void ActionCollapse(HierarchicalCity item)
        {
            item.IsExpanded = false;
            item.IsSelected = false;
        }

        private void ApplyActionToAllItems()//Action<HierarchicalCity> itemAction)
        {
            Stack<HierarchicalCity> dataItemStack = new Stack<HierarchicalCity>();
            dataItemStack.Push(this);

            while (dataItemStack.Count != 0)
            {
                HierarchicalCity currentItem = dataItemStack.Pop();
                ActionCollapse(currentItem);//itemAction(currentItem);
                foreach (HierarchicalCity childItem in currentItem.SubClass)
                {
                    dataItemStack.Push(childItem);
                }
            }
        }

        private bool ApplyActionToSuperclasses(HierarchicalCity itemToLookFor, Action<HierarchicalCity> itemAction)
        {
            if (itemToLookFor == this)
            {
                return true;
            }
            else
            {
                foreach (HierarchicalCity subclass in this.SubClass)
                {
                    bool foundItem = subclass.ApplyActionToSuperclasses(itemToLookFor, itemAction);
                    if (foundItem)
                    {
                        itemAction(this);
                        return true;
                    }
                }
                return false;
            }
        }

        #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 Code Project Open License (CPOL)


Written By
Team Leader Phoenix Tech Labs
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions