TreeViewModel.cs 6.5 KB

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