TreeNodeViewModel.cs 5.4 KB

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