|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.