TreeNodeViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. double x = 0;
  110. this.SetProperty(ref x, value);
  111. this.treeNode.X = value;
  112. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  113. foreach (TreeNodeViewModel child in this.Children)
  114. {
  115. child.ConnectorX2 = Width / 2 + this.treeNode.X - child.X;
  116. }
  117. }
  118. }
  119. public double Y
  120. {
  121. get
  122. {
  123. return this.treeNode.Y;
  124. }
  125. set
  126. {
  127. if (Math.Abs(this.treeNode.Y - value) < 0.1)
  128. {
  129. return;
  130. }
  131. double y = 0;
  132. this.SetProperty(ref y, value);
  133. this.treeNode.Y = value;
  134. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  135. foreach (var child in this.Children)
  136. {
  137. child.ConnectorY2 = Height + this.treeNode.Y - child.Y;
  138. }
  139. }
  140. }
  141. public double ConnectorX1
  142. {
  143. get
  144. {
  145. return Width / 2;
  146. }
  147. }
  148. public double ConnectorY1
  149. {
  150. get
  151. {
  152. return 0;
  153. }
  154. }
  155. public double ConnectorX2
  156. {
  157. get
  158. {
  159. return this.IsRoot? Width / 2 : this.connectorX2;
  160. }
  161. set
  162. {
  163. this.SetProperty(ref this.connectorX2, value);
  164. }
  165. }
  166. public double ConnectorY2
  167. {
  168. get
  169. {
  170. return this.IsRoot? 0 : this.connectorY2;
  171. }
  172. set
  173. {
  174. this.SetProperty(ref this.connectorY2, value);
  175. }
  176. }
  177. public int Type
  178. {
  179. get
  180. {
  181. return this.treeNode.Type;
  182. }
  183. set
  184. {
  185. if (this.treeNode.Type == value)
  186. {
  187. return;
  188. }
  189. int type = 0;
  190. this.SetProperty(ref type, value);
  191. this.treeNode.Type = value;
  192. }
  193. }
  194. public TreeNodeViewModel Parent
  195. {
  196. get
  197. {
  198. return this.parent;
  199. }
  200. set
  201. {
  202. this.parent = value;
  203. }
  204. }
  205. public ObservableCollection<TreeNodeViewModel> Children
  206. {
  207. get
  208. {
  209. return this.children;
  210. }
  211. set
  212. {
  213. this.children = value;
  214. }
  215. }
  216. public TreeNodeViewModel LeftSibling
  217. {
  218. get
  219. {
  220. if (this.IsRoot)
  221. {
  222. return null;
  223. }
  224. int index = this.Parent.Children.IndexOf(this);
  225. return index == 0? null : this.Parent.Children[index - 1];
  226. }
  227. }
  228. public TreeNodeViewModel LastChild
  229. {
  230. get
  231. {
  232. if (this.Children.Count == 0)
  233. {
  234. return null;
  235. }
  236. int maxIndex = this.Children.Count - 1;
  237. return this.Children[maxIndex];
  238. }
  239. }
  240. public TreeNodeViewModel FirstChild
  241. {
  242. get
  243. {
  244. return this.Children.Count == 0? null : this.Children[0];
  245. }
  246. }
  247. public bool IsLeaf
  248. {
  249. get
  250. {
  251. return this.Children.Count == 0;
  252. }
  253. }
  254. public double AncestorModify
  255. {
  256. get
  257. {
  258. return this.ancestorModify;
  259. }
  260. set
  261. {
  262. this.ancestorModify = value;
  263. }
  264. }
  265. }
  266. }