TreeNodeViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using Microsoft.Practices.Prism.Mvvm;
  5. using Microsoft.Practices.Prism.ViewModel;
  6. namespace Tree
  7. {
  8. public class TreeNodeViewModel : BindableBase
  9. {
  10. private static int globalNum;
  11. private readonly int num;
  12. private static double width = 80;
  13. private static double height = 50;
  14. private readonly TreeNode treeNode;
  15. private double connectorX2;
  16. private double connectorY2;
  17. private double prelim;
  18. private double modify;
  19. private double ancestorModify;
  20. private TreeNodeViewModel parent;
  21. private ObservableCollection<TreeNodeViewModel> children =
  22. new ObservableCollection<TreeNodeViewModel>();
  23. public TreeNodeViewModel(TreeNode treeNode, TreeNodeViewModel parent)
  24. {
  25. this.num = globalNum++;
  26. this.treeNode = treeNode;
  27. this.parent = parent ?? this;
  28. if (this.parent == this)
  29. {
  30. this.connectorX2 = 0;
  31. this.connectorY2 = Height / 2;
  32. }
  33. else
  34. {
  35. this.connectorX2 = Width + this.Parent.X - this.X;
  36. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  37. }
  38. }
  39. public int Num
  40. {
  41. get
  42. {
  43. return this.num;
  44. }
  45. }
  46. public static double Width
  47. {
  48. get
  49. {
  50. return width;
  51. }
  52. set
  53. {
  54. width = value;
  55. }
  56. }
  57. public static double Height
  58. {
  59. get
  60. {
  61. return height;
  62. }
  63. set
  64. {
  65. height = value;
  66. }
  67. }
  68. public bool IsRoot
  69. {
  70. get
  71. {
  72. return this.Parent == this;
  73. }
  74. }
  75. public double Prelim
  76. {
  77. get
  78. {
  79. return this.prelim;
  80. }
  81. set
  82. {
  83. this.prelim = value;
  84. }
  85. }
  86. public double Modify
  87. {
  88. get
  89. {
  90. return this.modify;
  91. }
  92. set
  93. {
  94. this.modify = value;
  95. }
  96. }
  97. public double X
  98. {
  99. get
  100. {
  101. return this.treeNode.X;
  102. }
  103. set
  104. {
  105. if (Math.Abs(this.treeNode.X - value) < 0.1)
  106. {
  107. return;
  108. }
  109. this.treeNode.X = value;
  110. this.OnPropertyChanged("X");
  111. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  112. foreach (TreeNodeViewModel child in this.Children)
  113. {
  114. child.ConnectorX2 = Width / 2 + this.treeNode.X - child.X;
  115. }
  116. }
  117. }
  118. public double Y
  119. {
  120. get
  121. {
  122. return this.treeNode.Y;
  123. }
  124. set
  125. {
  126. if (Math.Abs(this.treeNode.Y - value) < 0.1)
  127. {
  128. return;
  129. }
  130. this.treeNode.Y = value;
  131. this.OnPropertyChanged("Y");
  132. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  133. foreach (var child in this.Children)
  134. {
  135. child.ConnectorY2 = Height + this.treeNode.Y - child.Y;
  136. }
  137. }
  138. }
  139. public double ConnectorX1
  140. {
  141. get
  142. {
  143. return Width / 2;
  144. }
  145. }
  146. public double ConnectorY1
  147. {
  148. get
  149. {
  150. return 0;
  151. }
  152. }
  153. public double ConnectorX2
  154. {
  155. get
  156. {
  157. return this.IsRoot? Width / 2 : this.connectorX2;
  158. }
  159. set
  160. {
  161. this.SetProperty(ref this.connectorX2, value);
  162. }
  163. }
  164. public double ConnectorY2
  165. {
  166. get
  167. {
  168. return this.IsRoot? 0 : this.connectorY2;
  169. }
  170. set
  171. {
  172. this.SetProperty(ref this.connectorY2, value);
  173. }
  174. }
  175. public int Type
  176. {
  177. get
  178. {
  179. return this.treeNode.Type;
  180. }
  181. set
  182. {
  183. if (this.treeNode.Type == value)
  184. {
  185. return;
  186. }
  187. int type = 0;
  188. this.SetProperty(ref type, value);
  189. this.treeNode.Type = value;
  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. }