TreeNodeViewModel.cs 6.1 KB

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