TabablzRegionBehavior.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Dragablz;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Views;
  4. using Prism.Events;
  5. using Prism.Regions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Collections.Specialized;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. namespace PDF_Office.CustomControl.SystemControl
  16. {
  17. public class DragablzWindowBehavior : RegionBehavior
  18. {
  19. public const string BehaviorKey = "DragablzWindowBehavior";
  20. private TabablzControl activeTabcontrol;
  21. private readonly ObservableCollection<TabablzControl> tabcontrols;
  22. public DragablzWindowBehavior()
  23. {
  24. this.tabcontrols = new ObservableCollection<TabablzControl>();
  25. }
  26. protected override void OnAttach()
  27. {
  28. this.Region.Views.CollectionChanged += Views_CollectionChanged;
  29. this.Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged;
  30. var eventAggregator = Prism.Ioc.ContainerLocator.Container.Resolve(typeof(IEventAggregator)) as IEventAggregator;
  31. eventAggregator.GetEvent<DragablzWindowEvent>().Subscribe(OnDragablzWindowEvent);
  32. }
  33. public void OnDragablzWindowEvent(DragablzWindowEventArgs args)
  34. {
  35. switch (args.Type)
  36. {
  37. case DragablzWindowEventType.Opened:
  38. OnWindowOpened(args.TabControl);
  39. break;
  40. case DragablzWindowEventType.Closed:
  41. OnWindowClosed(args.TabControl);
  42. break;
  43. case DragablzWindowEventType.Activated:
  44. OnWindowActivated(args.TabControl);
  45. break;
  46. }
  47. }
  48. private void OnWindowActivated(TabablzControl tabControl)
  49. {
  50. if (this.activeTabcontrol != tabControl)
  51. {
  52. SetActiveView(tabControl);
  53. }
  54. }
  55. private void OnWindowClosed(TabablzControl tabControl)
  56. {
  57. ClearRelatedTabs(tabControl);
  58. this.tabcontrols.Remove(tabControl);
  59. tabControl.SelectionChanged -= TabControl_SelectionChanged;
  60. if (this.activeTabcontrol == tabControl)
  61. {
  62. this.activeTabcontrol = this.tabcontrols.FirstOrDefault();
  63. }
  64. }
  65. private void ClearRelatedTabs(TabablzControl tabControl)
  66. {
  67. var items = tabControl.Items.OfType<MainContent>().ToList();
  68. items.ForEach(item =>
  69. {
  70. try
  71. {
  72. this.Region.Remove(item.Content);
  73. }
  74. catch (ArgumentException) { }
  75. });
  76. }
  77. private void OnWindowOpened(TabablzControl tabControl)
  78. {
  79. this.activeTabcontrol = tabControl;
  80. this.tabcontrols.Add(tabControl);
  81. tabControl.ClosingItemCallback = ClosingItemCallback;
  82. tabControl.SelectionChanged += TabControl_SelectionChanged;
  83. }
  84. private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  85. {
  86. if (e.AddedItems.Count > 0)
  87. {
  88. var item = e.AddedItems[0] as MainContent;
  89. if (item != null)
  90. {
  91. var regionItem = item.Content;
  92. if (this.Region.Views.Contains(regionItem))
  93. {
  94. this.Region.Activate(regionItem);
  95. }
  96. }
  97. }
  98. }
  99. private void ClosingItemCallback(ItemActionCallbackArgs<TabablzControl> args)
  100. {
  101. //remove from region
  102. this.Region.Remove(((MainContent)args.DragablzItem.Content));
  103. }
  104. private void ActiveViews_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  105. {
  106. switch (e.Action)
  107. {
  108. case NotifyCollectionChangedAction.Add:
  109. ActivateView(e.NewItems[0]);
  110. break;
  111. }
  112. }
  113. private void Views_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  114. {
  115. switch (e.Action)
  116. {
  117. case NotifyCollectionChangedAction.Add:
  118. AddView(e.NewItems[0]);
  119. break;
  120. case NotifyCollectionChangedAction.Remove:
  121. RemoveView(e.OldItems[0]);
  122. break;
  123. }
  124. }
  125. public void ActivateView(object view)
  126. {
  127. var proxy = GetView(view);
  128. var tabcontrol = GetTabcontrol(view);
  129. if (tabcontrol.SelectedItem != proxy || tabcontrol != this.activeTabcontrol)
  130. {
  131. tabcontrol.SelectedItem = proxy;
  132. tabcontrol.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  133. }
  134. }
  135. private void SetActiveView(TabablzControl window)
  136. {
  137. if (this.activeTabcontrol != window)
  138. {
  139. this.activeTabcontrol = window;
  140. this.activeTabcontrol.BringIntoView();
  141. this.activeTabcontrol.Focus();
  142. var view = this.activeTabcontrol.SelectedItem as MainContent;
  143. if (view != null && this.Region.Views.Contains(view.Content))
  144. {
  145. this.Region.Activate(view.Content);
  146. }
  147. }
  148. }
  149. private void RemoveView(object view)
  150. {
  151. var tabcontrol = GetTabcontrol(view);
  152. var proxy = GetView(view);
  153. tabcontrol.Items.Remove(proxy);
  154. }
  155. private void AddView(object view)
  156. {
  157. activeTabcontrol = (App.Current.MainWindow as MainWindow).TabablzControl;
  158. var item = view as MainContent;
  159. if (item != null)
  160. {
  161. this.activeTabcontrol.Items.Add(item);
  162. this.activeTabcontrol.SelectedItem = item;
  163. }
  164. }
  165. private TabablzControl GetTabcontrol(object view)
  166. {
  167. var proxy = GetView(view);
  168. foreach (var window in this.tabcontrols)
  169. {
  170. if (ContainsView(window, proxy))
  171. {
  172. return window;
  173. }
  174. }
  175. return null;
  176. }
  177. private bool ContainsView(TabablzControl window, MainContent proxy)
  178. {
  179. if (proxy == null || window == null) return false;
  180. return window.Items.OfType<MainContent>().Any(tc => tc == proxy);
  181. }
  182. private MainContent GetView(object view)
  183. {
  184. return view as MainContent;
  185. }
  186. }
  187. }