DynamicTabControl.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Microsoft.Xaml.Behaviors.Core;
  18. namespace InABox.DynamicGrid
  19. {
  20. public class DynamicTabControlEventArgs : CancelEventArgs
  21. {
  22. public DynamicTabItem TabItem { get; set; }
  23. }
  24. public class DynamicTabItemContextMenuEventArgs : CancelEventArgs
  25. {
  26. public ContextMenu Menu { get; set; }
  27. }
  28. public class DynamicTabItemRenamedEventArgs : CancelEventArgs
  29. {
  30. public String OldName { get; set; }
  31. public String NewName { get; set; }
  32. }
  33. public delegate void DynamicTabControlChangedEvent(object sender, EventArgs args);
  34. public delegate void DynamicTabControlCreateEvent(object sender, DynamicTabControlEventArgs args);
  35. public delegate void DynamicTabItemCloseEvent(object sender, DynamicTabControlEventArgs args);
  36. public delegate void DynamicTabItemContextMenuEvent(object sender, DynamicTabItemContextMenuEventArgs args);
  37. public delegate void DynamicTabItemRenamedEvent(object sender, DynamicTabItemRenamedEventArgs args);
  38. public class DynamicTabControl : TabControl
  39. {
  40. public event DynamicTabControlChangedEvent OnTabsChanged;
  41. public static readonly DependencyProperty ChangedCommandProperty =
  42. DependencyProperty.Register(nameof(ChangedCommand), typeof(ICommand), typeof(DynamicTabControl));
  43. public ICommand ChangedCommand
  44. {
  45. get { return base.GetValue(ChangedCommandProperty) as ICommand; }
  46. set { base.SetValue(ChangedCommandProperty, value); }
  47. }
  48. public event DynamicTabControlCreateEvent OnCreateTab;
  49. public static readonly DependencyProperty CreateTabCommandProperty =
  50. DependencyProperty.Register(nameof(CreateTabCommand), typeof(ICommand), typeof(DynamicTabControl));
  51. public ICommand CreateTabCommand
  52. {
  53. get { return base.GetValue(CreateTabCommandProperty) as ICommand; }
  54. set { base.SetValue(CreateTabCommandProperty, value); }
  55. }
  56. public static readonly DependencyProperty CanCreateTabProperty =
  57. DependencyProperty.Register(nameof(CanCreateTab), typeof(bool), typeof(DynamicTabControl), new PropertyMetadata(false));
  58. public bool CanCreateTab
  59. {
  60. get => (bool)GetValue(CanCreateTabProperty);
  61. set => SetValue(CanCreateTabProperty, value);
  62. }
  63. public static readonly DependencyProperty LeftPanelProperty =
  64. DependencyProperty.Register(nameof(LeftPanel), typeof(FrameworkElement), typeof(DynamicTabControl), new UIPropertyMetadata(null));
  65. public FrameworkElement LeftPanel
  66. {
  67. get => (FrameworkElement)GetValue(LeftPanelProperty);
  68. set => SetValue(LeftPanelProperty, value);
  69. }
  70. public static readonly DependencyProperty RightPanelProperty =
  71. DependencyProperty.Register(nameof(RightPanel), typeof(FrameworkElement), typeof(DynamicTabControl), new UIPropertyMetadata(null));
  72. public FrameworkElement RightPanel
  73. {
  74. get => (FrameworkElement)GetValue(RightPanelProperty);
  75. set => SetValue(RightPanelProperty, value);
  76. }
  77. public DynamicTabItem? SelectedTab => (SelectedItem as DynamicTabItem)!;
  78. static DynamicTabControl()
  79. {
  80. DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabControl), new FrameworkPropertyMetadata(typeof(DynamicTabControl)));
  81. }
  82. public DynamicTabControl() : base()
  83. {
  84. ChangedCommand = new ActionCommand(() => OnTabsChanged?.Invoke(this, new EventArgs()));
  85. CanCreateTab = false;
  86. CreateTabCommand = new ActionCommand(() =>
  87. {
  88. var args = new DynamicTabControlEventArgs() { TabItem = new DynamicTabItem() { Header = "New TabItem" }};
  89. OnCreateTab?.Invoke(this, args);
  90. if (args.Cancel == false)
  91. {
  92. Items.Add(args.TabItem);
  93. ChangedCommand.Execute(null);
  94. }
  95. });
  96. }
  97. protected override DependencyObject GetContainerForItemOverride()
  98. {
  99. return new DynamicTabItem();
  100. }
  101. public static readonly DependencyProperty TabStripVisibleProperty =
  102. DependencyProperty.Register(nameof(TabStripVisible), typeof(bool), typeof(DynamicTabControl), new PropertyMetadata(true));
  103. private double _tabStripHeight;
  104. public bool TabStripVisible
  105. {
  106. get => (bool)GetValue(TabStripVisibleProperty);
  107. set
  108. {
  109. SetValue(TabStripVisibleProperty, value);
  110. Style s = new Style();
  111. s.Setters.Add(new Setter(UIElement.VisibilityProperty, value ? Visibility.Visible : Visibility.Collapsed));
  112. ItemContainerStyle = s;
  113. if (!value)
  114. {
  115. _tabStripHeight = TabStripHeight;
  116. TabStripHeight = 0;
  117. }
  118. else if(TabStripHeight == 0)
  119. {
  120. TabStripHeight = _tabStripHeight;
  121. }
  122. }
  123. }
  124. public static readonly DependencyProperty SeparatorMarginProperty =
  125. DependencyProperty.Register(nameof(SeparatorMargin), typeof(double), typeof(DynamicTabControl), new PropertyMetadata(1.5));
  126. public double SeparatorMargin
  127. {
  128. get => (double)GetValue(SeparatorMarginProperty);
  129. set => SetValue(SeparatorMarginProperty, value);
  130. }
  131. public static readonly DependencyProperty TabStripHeightProperty =
  132. DependencyProperty.Register(nameof(TabStripHeight), typeof(double), typeof(DynamicTabControl), new PropertyMetadata(25.0));
  133. public double TabStripHeight
  134. {
  135. get => (double)GetValue(TabStripHeightProperty);
  136. set => SetValue(TabStripHeightProperty, value);
  137. }
  138. }
  139. }