TreeNodeViewModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Practices.Prism.Mvvm;
  4. namespace Tree
  5. {
  6. public class TreeNodeViewModel: BindableBase
  7. {
  8. private static int globalNum;
  9. private static double width = 80;
  10. private static double height = 50;
  11. private TreeViewModel treeViewModel;
  12. private double x;
  13. private double y;
  14. private readonly TreeNodeData treeNodeData;
  15. private double connectorX2;
  16. private double connectorY2;
  17. private double prelim;
  18. private double modify;
  19. private double ancestorModify;
  20. private bool isFolder;
  21. public TreeNodeViewModel(TreeViewModel treeViewModel, double x, double y)
  22. {
  23. this.treeViewModel = treeViewModel;
  24. this.x = x;
  25. this.y = y;
  26. this.treeNodeData = new TreeNodeData();
  27. this.treeNodeData.Id = globalNum++;
  28. this.treeNodeData.Parent = 0;
  29. this.connectorX2 = 0;
  30. this.connectorY2 = Height / 2;
  31. }
  32. public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeViewModel parent)
  33. {
  34. this.treeViewModel = treeViewModel;
  35. this.treeNodeData = new TreeNodeData();
  36. this.treeNodeData.Id = globalNum++;
  37. this.Parent = parent;
  38. this.connectorX2 = Width + this.Parent.X - this.X;
  39. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  40. }
  41. public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeData data, TreeNodeViewModel parent)
  42. {
  43. this.treeViewModel = treeViewModel;
  44. this.treeNodeData = data;
  45. if (this.IsRoot)
  46. {
  47. this.x = 200;
  48. this.y = 10;
  49. this.connectorX2 = 0;
  50. this.connectorY2 = Height / 2;
  51. }
  52. else
  53. {
  54. this.connectorX2 = Width + this.Parent.X - this.X;
  55. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  56. }
  57. }
  58. public TreeNodeData TreeNodeData
  59. {
  60. get
  61. {
  62. this.treeNodeData.Children.Clear();
  63. foreach (int child in this.Children)
  64. {
  65. this.treeNodeData.Children.Add(child);
  66. }
  67. this.treeNodeData.Parent = this.IsRoot? 0 : this.Parent.Id;
  68. return this.treeNodeData;
  69. }
  70. }
  71. public int Id
  72. {
  73. get
  74. {
  75. return this.treeNodeData.Id;
  76. }
  77. set
  78. {
  79. this.treeNodeData.Id = value;
  80. this.OnPropertyChanged("Id");
  81. }
  82. }
  83. public string Comment
  84. {
  85. get
  86. {
  87. return this.treeNodeData.Comment;
  88. }
  89. set
  90. {
  91. this.treeNodeData.Comment = value;
  92. this.OnPropertyChanged("Comment");
  93. }
  94. }
  95. public static double Width
  96. {
  97. get
  98. {
  99. return width;
  100. }
  101. set
  102. {
  103. width = value;
  104. }
  105. }
  106. public static double Height
  107. {
  108. get
  109. {
  110. return height;
  111. }
  112. set
  113. {
  114. height = value;
  115. }
  116. }
  117. public bool IsRoot
  118. {
  119. get
  120. {
  121. return this.Parent == null;
  122. }
  123. }
  124. public double Prelim
  125. {
  126. get
  127. {
  128. return this.prelim;
  129. }
  130. set
  131. {
  132. this.prelim = value;
  133. }
  134. }
  135. public double Modify
  136. {
  137. get
  138. {
  139. return this.modify;
  140. }
  141. set
  142. {
  143. this.modify = value;
  144. }
  145. }
  146. public double X
  147. {
  148. get
  149. {
  150. return this.x;
  151. }
  152. set
  153. {
  154. if (Math.Abs(this.x - value) < 0.1)
  155. {
  156. return;
  157. }
  158. this.x = value;
  159. this.OnPropertyChanged("X");
  160. if (this.Parent != null)
  161. {
  162. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  163. }
  164. foreach (var childId in this.Children)
  165. {
  166. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  167. child.ConnectorX2 = Width / 2 + this.X - child.X;
  168. }
  169. }
  170. }
  171. public double Y
  172. {
  173. get
  174. {
  175. return this.y;
  176. }
  177. set
  178. {
  179. if (Math.Abs(this.Y - value) < 0.1)
  180. {
  181. return;
  182. }
  183. this.y = value;
  184. this.OnPropertyChanged("Y");
  185. if (this.Parent != null)
  186. {
  187. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  188. }
  189. foreach (var childId in this.Children)
  190. {
  191. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  192. child.ConnectorY2 = Height + this.Y - child.Y;
  193. }
  194. }
  195. }
  196. public double ConnectorX1
  197. {
  198. get
  199. {
  200. return Width / 2;
  201. }
  202. }
  203. public double ConnectorY1
  204. {
  205. get
  206. {
  207. return 0;
  208. }
  209. }
  210. public double ConnectorX2
  211. {
  212. get
  213. {
  214. return this.IsRoot? Width / 2 : this.connectorX2;
  215. }
  216. set
  217. {
  218. this.SetProperty(ref this.connectorX2, value);
  219. }
  220. }
  221. public double ConnectorY2
  222. {
  223. get
  224. {
  225. return this.IsRoot? 0 : this.connectorY2;
  226. }
  227. set
  228. {
  229. this.SetProperty(ref this.connectorY2, value);
  230. }
  231. }
  232. public int Type
  233. {
  234. get
  235. {
  236. return this.treeNodeData.Type;
  237. }
  238. set
  239. {
  240. if (this.treeNodeData.Type == value)
  241. {
  242. return;
  243. }
  244. this.treeNodeData.Type = value;
  245. this.OnPropertyChanged("Type");
  246. }
  247. }
  248. public List<string> Args
  249. {
  250. get
  251. {
  252. return this.treeNodeData.Args;
  253. }
  254. set
  255. {
  256. if (this.treeNodeData.Args == value)
  257. {
  258. return;
  259. }
  260. this.treeNodeData.Args = value;
  261. this.OnPropertyChanged("Args");
  262. }
  263. }
  264. public TreeNodeViewModel Parent
  265. {
  266. get
  267. {
  268. if (this.Id == 0)
  269. {
  270. return null;
  271. }
  272. TreeNodeViewModel parent = this.treeViewModel.Get(this.treeNodeData.Parent);
  273. return parent;
  274. }
  275. set
  276. {
  277. if (value == null)
  278. {
  279. this.treeNodeData.Parent = 0;
  280. }
  281. this.treeNodeData.Parent = value.Id;
  282. }
  283. }
  284. /// <summary>
  285. /// 节点是否折叠
  286. /// </summary>
  287. public bool IsFolder
  288. {
  289. get
  290. {
  291. return this.isFolder;
  292. }
  293. set
  294. {
  295. if (this.isFolder == value)
  296. {
  297. return;
  298. }
  299. this.isFolder = value;
  300. this.OnPropertyChanged("IsFolder");
  301. }
  302. }
  303. public List<int> Children
  304. {
  305. get
  306. {
  307. return this.treeNodeData.Children;
  308. }
  309. }
  310. public TreeNodeViewModel LeftSibling
  311. {
  312. get
  313. {
  314. if (this.IsRoot)
  315. {
  316. return null;
  317. }
  318. int index = this.Parent.Children.IndexOf(this.Id);
  319. return index == 0? null : this.treeViewModel.Get(this.Parent.Children[index - 1]);
  320. }
  321. }
  322. public TreeNodeViewModel LastChild
  323. {
  324. get
  325. {
  326. if (this.Children.Count == 0)
  327. {
  328. return null;
  329. }
  330. int maxIndex = this.Children.Count - 1;
  331. return this.treeViewModel.Get(this.Children[maxIndex]);
  332. }
  333. }
  334. public TreeNodeViewModel FirstChild
  335. {
  336. get
  337. {
  338. return this.Children.Count == 0? null : this.treeViewModel.Get(this.Children[0]);
  339. }
  340. }
  341. public bool IsLeaf
  342. {
  343. get
  344. {
  345. return this.Children.Count == 0;
  346. }
  347. }
  348. public double AncestorModify
  349. {
  350. get
  351. {
  352. return this.ancestorModify;
  353. }
  354. set
  355. {
  356. this.ancestorModify = value;
  357. }
  358. }
  359. }
  360. }