TreeViewModel.cs 6.6 KB

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