TreeNodeViewModel.cs 4.2 KB

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