TreeViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel.Composition;
  4. using Microsoft.Practices.Prism.Mvvm;
  5. namespace Modules.Tree
  6. {
  7. [Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
  8. public class TreeViewModel: BindableBase
  9. {
  10. private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
  11. new ObservableCollection<TreeNodeViewModel>();
  12. private readonly Dictionary<int, TreeNodeViewModel> treeNodeDict = new Dictionary<int, TreeNodeViewModel>();
  13. public ObservableCollection<TreeNodeViewModel> TreeNodes
  14. {
  15. get
  16. {
  17. return this.treeNodes;
  18. }
  19. }
  20. public int TreeId { get; set; }
  21. public TreeViewModel(AllTreeViewModel allTreeViewModel)
  22. {
  23. this.AllTreeViewModel = allTreeViewModel;
  24. this.TreeId = ++allTreeViewModel.MaxTreeId;
  25. TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, 300, 100);
  26. this.treeNodes.Add(treeNodeViewModel);
  27. this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
  28. var treeLayout = new TreeLayout(this);
  29. treeLayout.ExcuteLayout(this.Root);
  30. }
  31. public TreeViewModel(List<TreeNodeData> treeNodeDatas)
  32. {
  33. foreach (TreeNodeData treeNodeData in treeNodeDatas)
  34. {
  35. TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, treeNodeData);
  36. this.treeNodes.Add(treeNodeViewModel);
  37. this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
  38. }
  39. var treeLayout = new TreeLayout(this);
  40. treeLayout.ExcuteLayout(this.Root);
  41. }
  42. public List<TreeNodeData> GetDatas()
  43. {
  44. List<TreeNodeData> treeNodeDatas = new List<TreeNodeData>();
  45. foreach (TreeNodeViewModel treeNodeViewModel in treeNodes)
  46. {
  47. treeNodeDatas.Add(treeNodeViewModel.Data);
  48. }
  49. return treeNodeDatas;
  50. }
  51. public AllTreeViewModel AllTreeViewModel { get; set; }
  52. public TreeNodeViewModel Root
  53. {
  54. get
  55. {
  56. return this.treeNodes.Count == 0? null : this.treeNodes[0];
  57. }
  58. }
  59. public TreeNodeViewModel Get(int id)
  60. {
  61. TreeNodeViewModel node;
  62. this.treeNodeDict.TryGetValue(id, out node);
  63. return node;
  64. }
  65. public void Add(TreeNodeViewModel treeNode, TreeNodeViewModel parent)
  66. {
  67. // 如果父节点是折叠的,需要先展开父节点
  68. if (parent != null && parent.IsFold)
  69. {
  70. this.UnFold(parent);
  71. }
  72. this.treeNodes.Add(treeNode);
  73. this.treeNodeDict[treeNode.Id] = treeNode;
  74. if (parent != null)
  75. {
  76. parent.Children.Add(treeNode.Id);
  77. }
  78. var treeLayout = new TreeLayout(this);
  79. treeLayout.ExcuteLayout(this.Root);
  80. }
  81. private void GetChildrenIdAndSelf(TreeNodeViewModel treeNodeViewModel, List<int> children)
  82. {
  83. children.Add(treeNodeViewModel.Id);
  84. this.GetAllChildrenId(treeNodeViewModel, children);
  85. }
  86. private void GetAllChildrenId(TreeNodeViewModel treeNodeViewModel, List<int> children)
  87. {
  88. foreach (int childId in treeNodeViewModel.Children)
  89. {
  90. TreeNodeViewModel child = this.Get(childId);
  91. children.Add(child.Id);
  92. this.GetAllChildrenId(child, children);
  93. }
  94. }
  95. public void Remove(TreeNodeViewModel treeNodeViewModel)
  96. {
  97. List<int> allId = new List<int>();
  98. this.GetChildrenIdAndSelf(treeNodeViewModel, allId);
  99. foreach (int childId in allId)
  100. {
  101. TreeNodeViewModel child = this.Get(childId);
  102. this.treeNodes.Remove(child);
  103. this.treeNodes.Remove(treeNodeViewModel);
  104. }
  105. TreeNodeViewModel parent = treeNodeViewModel.Parent;
  106. if (parent != null)
  107. {
  108. parent.Children.Remove(treeNodeViewModel.Id);
  109. }
  110. var treeLayout = new TreeLayout(this);
  111. treeLayout.ExcuteLayout(this.Root);
  112. }
  113. private void RecursionMove(
  114. TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
  115. {
  116. treeNodeViewModel.X += offsetX;
  117. treeNodeViewModel.Y += offsetY;
  118. foreach (var childId in treeNodeViewModel.Children)
  119. {
  120. TreeNodeViewModel child = this.Get(childId);
  121. this.RecursionMove(child, offsetX, offsetY);
  122. }
  123. }
  124. public void MoveToPosition(double offsetX, double offsetY)
  125. {
  126. this.RecursionMove(this.Root, offsetX, offsetY);
  127. }
  128. public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
  129. {
  130. // from节点不能是to节点的父级节点
  131. TreeNodeViewModel tmpNode = to;
  132. while (tmpNode != null)
  133. {
  134. if (tmpNode.IsRoot)
  135. {
  136. break;
  137. }
  138. if (tmpNode.Id == from.Id)
  139. {
  140. return;
  141. }
  142. tmpNode = tmpNode.Parent;
  143. }
  144. if (from.IsFold)
  145. {
  146. this.UnFold(from);
  147. }
  148. if (to.IsFold)
  149. {
  150. this.UnFold(to);
  151. }
  152. from.Parent.Children.Remove(from.Id);
  153. to.Children.Add(from.Id);
  154. from.Parent = to;
  155. var treeLayout = new TreeLayout(this);
  156. treeLayout.ExcuteLayout(this.Root);
  157. }
  158. /// <summary>
  159. /// 折叠节点
  160. /// </summary>
  161. /// <param name="treeNodeViewModel"></param>
  162. public void Fold(TreeNodeViewModel treeNodeViewModel)
  163. {
  164. List<int> allChildId = new List<int>();
  165. this.GetAllChildrenId(treeNodeViewModel, allChildId);
  166. foreach (int childId in allChildId)
  167. {
  168. TreeNodeViewModel child = this.Get(childId);
  169. this.treeNodes.Remove(child);
  170. }
  171. treeNodeViewModel.IsFold = true;
  172. var treeLayout = new TreeLayout(this);
  173. treeLayout.ExcuteLayout(this.Root);
  174. }
  175. /// <summary>
  176. /// 展开节点,一级一级展开,一次只展开下层子节点,比如下层节点是折叠的,那下下层节点不展开
  177. /// </summary>
  178. /// <param name="treeNodeViewModel"></param>
  179. public void UnFold(TreeNodeViewModel treeNodeViewModel)
  180. {
  181. treeNodeViewModel.IsFold = false;
  182. List<int> allChildId = new List<int>();
  183. this.GetAllChildrenId(treeNodeViewModel, allChildId);
  184. foreach (int childId in allChildId)
  185. {
  186. TreeNodeViewModel child = this.Get(childId);
  187. this.treeNodes.Add(child);
  188. }
  189. var treeLayout = new TreeLayout(this);
  190. treeLayout.ExcuteLayout(this.Root);
  191. }
  192. }
  193. }