DynamicTabItem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using InABox.Wpf.Editors;
  6. using Microsoft.Xaml.Behaviors.Core;
  7. namespace InABox.DynamicGrid;
  8. public class DynamicTabItem : TabItem
  9. {
  10. public event DynamicTabItemCloseEvent OnCloseTab;
  11. public static readonly DependencyProperty CloseTabCommandProperty =
  12. DependencyProperty.Register(nameof(CloseTabCommand), typeof(ICommand), typeof(DynamicTabItem));
  13. public ICommand CloseTabCommand
  14. {
  15. get { return base.GetValue(CloseTabCommandProperty) as ICommand; }
  16. set { base.SetValue(CloseTabCommandProperty, value); }
  17. }
  18. public static readonly DependencyProperty CanCloseProperty =
  19. DependencyProperty.Register(nameof(CanClose), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
  20. public bool CanClose
  21. {
  22. get => (bool)GetValue(CanCloseProperty);
  23. set => SetValue(CanCloseProperty, value);
  24. }
  25. public event DynamicTabItemContextMenuEvent OnContextMenuOpening;
  26. public static readonly DependencyProperty ContextMenuCommandProperty =
  27. DependencyProperty.Register(nameof(ContextMenuCommand), typeof(ICommand), typeof(DynamicTabItem));
  28. public ICommand ContextMenuCommand
  29. {
  30. get { return base.GetValue(ContextMenuCommandProperty) as ICommand; }
  31. set { base.SetValue(ContextMenuCommandProperty, value); }
  32. }
  33. public static readonly DependencyProperty CanRenameProperty =
  34. DependencyProperty.Register(nameof(CanRename), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
  35. public bool CanRename
  36. {
  37. get => (bool)GetValue(CanRenameProperty);
  38. set => SetValue(CanRenameProperty, value);
  39. }
  40. /// <summary>
  41. /// Called when a tab is renamed. Note that changing <see cref="DynamicTabItemRenamedEventArgs.NewName"/> will change the new name of the tab.
  42. /// </summary>
  43. public event DynamicTabItemRenamedEvent OnTabRenamed;
  44. static DynamicTabItem()
  45. {
  46. DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabItem), new FrameworkPropertyMetadata(typeof(DynamicTabItem)));
  47. }
  48. public DynamicTabItem() : base()
  49. {
  50. VerticalAlignment = VerticalAlignment.Stretch;
  51. CloseTabCommand = new ActionCommand(() =>
  52. {
  53. bool bConfirm = false;
  54. if (OnCloseTab != null)
  55. {
  56. var args = new DynamicTabControlEventArgs() { TabItem = this };
  57. OnCloseTab?.Invoke(this, args);
  58. bConfirm = !args.Cancel;
  59. }
  60. else
  61. bConfirm = MessageBox.Show("Are you sure you wish to close this tab?", "Close Tab", MessageBoxButton.YesNo) ==
  62. MessageBoxResult.Yes;
  63. if (bConfirm)
  64. {
  65. var parent = Parent as DynamicTabControl;
  66. if (parent != null)
  67. {
  68. parent.Items.Remove(this);
  69. parent.ChangedCommand.Execute(null);
  70. }
  71. }
  72. });
  73. ContextMenuCommand = new ActionCommand(() =>
  74. {
  75. ContextMenu menu = new ContextMenu();
  76. if (CanRename)
  77. {
  78. menu.Items.Add(new MenuItem()
  79. {
  80. Header = "Rename Tab", Command = new ActionCommand(() =>
  81. {
  82. String tabname = Header.ToString() ?? "";
  83. if (TextEdit.Execute("Rename Tab", ref tabname))
  84. {
  85. var args = new DynamicTabItemRenamedEventArgs() { OldName = Header.ToString() ?? "", NewName = tabname };
  86. OnTabRenamed?.Invoke(this, args);
  87. Header = args.NewName;
  88. var parent = Parent as DynamicTabControl;
  89. if (parent != null)
  90. parent.ChangedCommand.Execute(null);
  91. }
  92. })
  93. });
  94. }
  95. if (OnContextMenuOpening != null)
  96. {
  97. var args = new DynamicTabItemContextMenuEventArgs { Menu = menu };
  98. OnContextMenuOpening.Invoke(this, args);
  99. if (args.Cancel == false)
  100. args.Menu.IsOpen = true;
  101. }
  102. else
  103. menu.IsOpen = CanRename;
  104. });
  105. }
  106. }