TreeNodeViewModel.cs 9.4 KB

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