TreeNodeViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections.ObjectModel;
  2. using Microsoft.Practices.Prism.ViewModel;
  3. namespace Tree
  4. {
  5. public class TreeNodeViewModel: NotificationObject
  6. {
  7. private static int globalNum;
  8. private readonly int num;
  9. private const double width = 80;
  10. private const double height = 50;
  11. private readonly TreeNode treeNode;
  12. private double connectorX2;
  13. private double connectorY2;
  14. private double prelim;
  15. private double modify;
  16. private double ancestorModify;
  17. private TreeNodeViewModel parent;
  18. private ObservableCollection<TreeNodeViewModel> children =
  19. new ObservableCollection<TreeNodeViewModel>();
  20. public TreeNodeViewModel(TreeNode treeNode, TreeNodeViewModel parent)
  21. {
  22. this.num = globalNum++;
  23. this.treeNode = treeNode;
  24. this.parent = parent ?? this;
  25. if (this.parent == this)
  26. {
  27. this.connectorX2 = 0;
  28. this.connectorY2 = Height / 2;
  29. }
  30. else
  31. {
  32. this.connectorX2 = Width + this.Parent.X - this.X;
  33. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  34. }
  35. }
  36. public int Num
  37. {
  38. get
  39. {
  40. return this.num;
  41. }
  42. }
  43. public static double Width
  44. {
  45. get
  46. {
  47. return width;
  48. }
  49. }
  50. public static double Height
  51. {
  52. get
  53. {
  54. return height;
  55. }
  56. }
  57. public bool IsRoot
  58. {
  59. get
  60. {
  61. return this.Parent == this;
  62. }
  63. }
  64. public double Prelim
  65. {
  66. get
  67. {
  68. return this.prelim;
  69. }
  70. set
  71. {
  72. this.prelim = value;
  73. }
  74. }
  75. public double Modify
  76. {
  77. get
  78. {
  79. return this.modify;
  80. }
  81. set
  82. {
  83. this.modify = value;
  84. }
  85. }
  86. public double X
  87. {
  88. get
  89. {
  90. return this.treeNode.X;
  91. }
  92. set
  93. {
  94. if (this.treeNode.X == value)
  95. {
  96. return;
  97. }
  98. this.treeNode.X = value;
  99. this.RaisePropertyChanged("X");
  100. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  101. foreach (TreeNodeViewModel child in this.Children)
  102. {
  103. child.ConnectorX2 = Width / 2 + this.treeNode.X - child.X;
  104. }
  105. }
  106. }
  107. public double Y
  108. {
  109. get
  110. {
  111. return this.treeNode.Y;
  112. }
  113. set
  114. {
  115. if (this.treeNode.Y == value)
  116. {
  117. return;
  118. }
  119. this.treeNode.Y = value;
  120. this.RaisePropertyChanged("Y");
  121. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  122. foreach (var child in this.Children)
  123. {
  124. child.ConnectorY2 = Height + this.treeNode.Y - child.Y;
  125. }
  126. }
  127. }
  128. public double ConnectorX1
  129. {
  130. get
  131. {
  132. return Width / 2;
  133. }
  134. }
  135. public double ConnectorY1
  136. {
  137. get
  138. {
  139. return 0;
  140. }
  141. }
  142. public double ConnectorX2
  143. {
  144. get
  145. {
  146. return this.IsRoot? width / 2 : this.connectorX2;
  147. }
  148. set
  149. {
  150. this.connectorX2 = value;
  151. this.RaisePropertyChanged("ConnectorX2");
  152. }
  153. }
  154. public double ConnectorY2
  155. {
  156. get
  157. {
  158. return this.IsRoot? 0 : this.connectorY2;
  159. }
  160. set
  161. {
  162. this.connectorY2 = value;
  163. this.RaisePropertyChanged("ConnectorY2");
  164. }
  165. }
  166. public int Type
  167. {
  168. get
  169. {
  170. return this.treeNode.Type;
  171. }
  172. set
  173. {
  174. if (this.treeNode.Type == value)
  175. {
  176. return;
  177. }
  178. this.treeNode.Type = value;
  179. this.RaisePropertyChanged("Type");
  180. }
  181. }
  182. public TreeNodeViewModel Parent
  183. {
  184. get
  185. {
  186. return this.parent;
  187. }
  188. set
  189. {
  190. this.parent = value;
  191. }
  192. }
  193. public ObservableCollection<TreeNodeViewModel> Children
  194. {
  195. get
  196. {
  197. return this.children;
  198. }
  199. set
  200. {
  201. this.children = value;
  202. }
  203. }
  204. public TreeNodeViewModel LeftSibling
  205. {
  206. get
  207. {
  208. if (this.IsRoot)
  209. {
  210. return null;
  211. }
  212. int index = this.Parent.Children.IndexOf(this);
  213. return index == 0? null : this.Parent.Children[index - 1];
  214. }
  215. }
  216. public TreeNodeViewModel LastChild
  217. {
  218. get
  219. {
  220. if (this.Children.Count == 0)
  221. {
  222. return null;
  223. }
  224. int maxIndex = this.Children.Count - 1;
  225. return this.Children[maxIndex];
  226. }
  227. }
  228. public TreeNodeViewModel FirstChild
  229. {
  230. get
  231. {
  232. return this.Children.Count == 0? null : this.Children[0];
  233. }
  234. }
  235. public bool IsLeaf
  236. {
  237. get
  238. {
  239. return this.Children.Count == 0;
  240. }
  241. }
  242. public double AncestorModify
  243. {
  244. get
  245. {
  246. return this.ancestorModify;
  247. }
  248. set
  249. {
  250. this.ancestorModify = value;
  251. }
  252. }
  253. }
  254. }