TreeNodeViewModel.cs 4.5 KB

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