TreeViewModel.cs 6.7 KB

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