TreeNodeViewModel.cs 4.4 KB

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