DynamicTabControl.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using InABox.Wpf.Editors;
  18. using Microsoft.Xaml.Behaviors.Core;
  19. namespace InABox.DynamicGrid
  20. {
  21. public class DynamicTabControlEventArgs : CancelEventArgs
  22. {
  23. public DynamicTabItem TabItem { get; set; }
  24. }
  25. public class DynamicTabItemContextMenuEventArgs : CancelEventArgs
  26. {
  27. public ContextMenu Menu { get; set; }
  28. }
  29. public class DynamicTabItemRenamedEventArgs : CancelEventArgs
  30. {
  31. public String OldName { get; set; }
  32. public String NewName { get; set; }
  33. }
  34. public delegate void DynamicTabControlChangedEvent(object sender, EventArgs args);
  35. public delegate void DynamicTabControlCreateEvent(object sender, DynamicTabControlEventArgs args);
  36. public delegate void DynamicTabItemCloseEvent(object sender, DynamicTabControlEventArgs args);
  37. public delegate void DynamicTabItemContextMenuEvent(object sender, DynamicTabItemContextMenuEventArgs args);
  38. public delegate void DynamicTabItemRenamedEvent(object sender, DynamicTabItemRenamedEventArgs args);
  39. public class DynamicTabControl : TabControl
  40. {
  41. public event DynamicTabControlChangedEvent OnTabsChanged;
  42. public static readonly DependencyProperty ChangedCommandProperty =
  43. DependencyProperty.Register(nameof(ChangedCommand), typeof(ICommand), typeof(DynamicTabControl));
  44. public ICommand ChangedCommand
  45. {
  46. get { return base.GetValue(ChangedCommandProperty) as ICommand; }
  47. set { base.SetValue(ChangedCommandProperty, value); }
  48. }
  49. public event DynamicTabControlCreateEvent OnCreateTab;
  50. public static readonly DependencyProperty CreateTabCommandProperty =
  51. DependencyProperty.Register(nameof(CreateTabCommand), typeof(ICommand), typeof(DynamicTabControl));
  52. public ICommand CreateTabCommand
  53. {
  54. get { return base.GetValue(CreateTabCommandProperty) as ICommand; }
  55. set { base.SetValue(CreateTabCommandProperty, value); }
  56. }
  57. public static readonly DependencyProperty CanCreateTabProperty =
  58. DependencyProperty.Register(nameof(CanCreateTab), typeof(bool), typeof(DynamicTabControl), new PropertyMetadata(false));
  59. public bool CanCreateTab
  60. {
  61. get => (bool)GetValue(CanCreateTabProperty);
  62. set => SetValue(CanCreateTabProperty, value);
  63. }
  64. static DynamicTabControl()
  65. {
  66. DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabControl), new FrameworkPropertyMetadata(typeof(DynamicTabControl)));
  67. }
  68. public DynamicTabControl() : base()
  69. {
  70. ChangedCommand = new ActionCommand(() => OnTabsChanged?.Invoke(this, new EventArgs()));
  71. CanCreateTab = false;
  72. CreateTabCommand = new ActionCommand(() =>
  73. {
  74. var args = new DynamicTabControlEventArgs() { TabItem = new DynamicTabItem() { Header = "New TabItem" }};
  75. OnCreateTab?.Invoke(this, args);
  76. if (args.Cancel == false)
  77. {
  78. Items.Add(args.TabItem);
  79. ChangedCommand.Execute(null);
  80. }
  81. });
  82. }
  83. protected override DependencyObject GetContainerForItemOverride()
  84. {
  85. return new DynamicTabItem();
  86. }
  87. }
  88. public class DynamicTabItem : TabItem
  89. {
  90. public event DynamicTabItemCloseEvent OnCloseTab;
  91. public static readonly DependencyProperty CloseTabCommandProperty =
  92. DependencyProperty.Register(nameof(CloseTabCommand), typeof(ICommand), typeof(DynamicTabItem));
  93. public ICommand CloseTabCommand
  94. {
  95. get { return base.GetValue(CloseTabCommandProperty) as ICommand; }
  96. set { base.SetValue(CloseTabCommandProperty, value); }
  97. }
  98. public static readonly DependencyProperty CanCloseProperty =
  99. DependencyProperty.Register(nameof(CanClose), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
  100. public bool CanClose
  101. {
  102. get => (bool)GetValue(CanCloseProperty);
  103. set => SetValue(CanCloseProperty, value);
  104. }
  105. public event DynamicTabItemContextMenuEvent OnContextMenuOpening;
  106. public static readonly DependencyProperty ContextMenuCommandProperty =
  107. DependencyProperty.Register(nameof(ContextMenuCommand), typeof(ICommand), typeof(DynamicTabItem));
  108. public ICommand ContextMenuCommand
  109. {
  110. get { return base.GetValue(ContextMenuCommandProperty) as ICommand; }
  111. set { base.SetValue(ContextMenuCommandProperty, value); }
  112. }
  113. public static readonly DependencyProperty CanRenameProperty =
  114. DependencyProperty.Register(nameof(CanRename), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
  115. public bool CanRename
  116. {
  117. get => (bool)GetValue(CanRenameProperty);
  118. set => SetValue(CanRenameProperty, value);
  119. }
  120. public event DynamicTabItemRenamedEvent OnTabRenamed;
  121. static DynamicTabItem()
  122. {
  123. DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabItem), new FrameworkPropertyMetadata(typeof(DynamicTabItem)));
  124. }
  125. public DynamicTabItem() : base()
  126. {
  127. CloseTabCommand = new ActionCommand(() =>
  128. {
  129. bool bConfirm = false;
  130. if (OnCloseTab != null)
  131. {
  132. var args = new DynamicTabControlEventArgs() { TabItem = this };
  133. OnCloseTab?.Invoke(this, args);
  134. bConfirm = !args.Cancel;
  135. }
  136. else
  137. bConfirm = MessageBox.Show("Are you sure you wish to close this tab?", "Close Tab", MessageBoxButton.YesNo) ==
  138. MessageBoxResult.Yes;
  139. if (bConfirm)
  140. {
  141. var parent = Parent as DynamicTabControl;
  142. if (parent != null)
  143. {
  144. parent.Items.Remove(this);
  145. parent.ChangedCommand.Execute(null);
  146. }
  147. }
  148. });
  149. ContextMenuCommand = new ActionCommand(() =>
  150. {
  151. ContextMenu menu = new ContextMenu();
  152. if (CanRename)
  153. {
  154. menu.Items.Add(new MenuItem()
  155. {
  156. Header = "Rename Tab", Command = new ActionCommand(() =>
  157. {
  158. String tabname = Header.ToString() ?? string.Empty;
  159. if (TextEdit.Execute("Rename Tab", ref tabname))
  160. {
  161. OnTabRenamed?.Invoke(this, new DynamicTabItemRenamedEventArgs() { OldName = Header.ToString() ?? string.Empty, NewName = tabname });
  162. Header = tabname;
  163. var parent = Parent as DynamicTabControl;
  164. if (parent != null)
  165. parent.ChangedCommand.Execute(null);
  166. }
  167. })
  168. });
  169. }
  170. if (OnContextMenuOpening != null)
  171. {
  172. var args = new DynamicTabItemContextMenuEventArgs { Menu = menu };
  173. OnContextMenuOpening.Invoke(this, args);
  174. if (args.Cancel == false)
  175. args.Menu.IsOpen = true;
  176. }
  177. else
  178. menu.IsOpen = CanRename;
  179. });
  180. }
  181. }
  182. }