TreeNodeViewModel.cs 9.8 KB

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