TreeViewItemViewModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. namespace Egametang
  4. {
  5. /// <summary>
  6. /// Base class for all ViewModel classes displayed by TreeViewItems.
  7. /// This acts as an adapter between a raw data object and a TreeViewItem.
  8. /// </summary>
  9. public class TreeViewItemViewModel : INotifyPropertyChanged
  10. {
  11. #region Data
  12. static readonly TreeViewItemViewModel dummyChild = new TreeViewItemViewModel();
  13. readonly ObservableCollection<TreeViewItemViewModel> children;
  14. readonly TreeViewItemViewModel parent;
  15. bool isExpanded;
  16. bool isSelected;
  17. #endregion // Data
  18. #region Constructors
  19. protected TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildren)
  20. {
  21. this.parent = parent;
  22. children = new ObservableCollection<TreeViewItemViewModel>();
  23. if (lazyLoadChildren)
  24. {
  25. children.Add(dummyChild);
  26. }
  27. }
  28. // This is used to create the DummyChild instance.
  29. private TreeViewItemViewModel()
  30. {
  31. }
  32. #endregion // Constructors
  33. #region Children
  34. /// <summary>
  35. /// Returns the logical child items of this object.
  36. /// </summary>
  37. public ObservableCollection<TreeViewItemViewModel> Children
  38. {
  39. get
  40. {
  41. return children;
  42. }
  43. }
  44. #endregion // Children
  45. #region HasLoadedChildren
  46. /// <summary>
  47. /// Returns true if this object's Children have not yet been populated.
  48. /// </summary>
  49. public bool HasDummyChild
  50. {
  51. get
  52. {
  53. return this.Children.Count == 1 && this.Children[0] == dummyChild;
  54. }
  55. }
  56. #endregion // HasLoadedChildren
  57. #region IsExpanded
  58. /// <summary>
  59. /// Gets/sets whether the TreeViewItem
  60. /// associated with this object is expanded.
  61. /// </summary>
  62. public bool IsExpanded
  63. {
  64. get
  65. {
  66. return isExpanded;
  67. }
  68. set
  69. {
  70. if (value != isExpanded)
  71. {
  72. isExpanded = value;
  73. this.OnPropertyChanged("IsExpanded");
  74. }
  75. // Expand all the way up to the root.
  76. if (isExpanded && parent != null)
  77. {
  78. parent.IsExpanded = true;
  79. }
  80. // Lazy load the child items, if necessary.
  81. if (this.HasDummyChild)
  82. {
  83. this.Children.Remove(dummyChild);
  84. this.LoadChildren();
  85. }
  86. }
  87. }
  88. #endregion // IsExpanded
  89. #region IsSelected
  90. /// <summary>
  91. /// Gets/sets whether the TreeViewItem
  92. /// associated with this object is selected.
  93. /// </summary>
  94. public bool IsSelected
  95. {
  96. get
  97. {
  98. return isSelected;
  99. }
  100. set
  101. {
  102. if (value != isSelected)
  103. {
  104. isSelected = value;
  105. this.OnPropertyChanged("IsSelected");
  106. }
  107. }
  108. }
  109. #endregion // IsSelected
  110. #region LoadChildren
  111. /// <summary>
  112. /// Invoked when the child items need to be loaded on demand.
  113. /// Subclasses can override this to populate the Children collection.
  114. /// </summary>
  115. protected virtual void LoadChildren()
  116. {
  117. }
  118. #endregion // LoadChildren
  119. #region Parent
  120. public TreeViewItemViewModel Parent
  121. {
  122. get
  123. {
  124. return parent;
  125. }
  126. }
  127. #endregion // Parent
  128. #region INotifyPropertyChanged Members
  129. public event PropertyChangedEventHandler PropertyChanged;
  130. protected virtual void OnPropertyChanged(string propertyName)
  131. {
  132. if (this.PropertyChanged != null)
  133. {
  134. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  135. }
  136. }
  137. #endregion // INotifyPropertyChanged Members
  138. }
  139. }