123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using InABox.Wpf.Editors;
- using Microsoft.Xaml.Behaviors.Core;
- namespace InABox.DynamicGrid
- {
- public class DynamicTabControlEventArgs : CancelEventArgs
- {
- public DynamicTabItem TabItem { get; set; }
- }
-
- public class DynamicTabItemContextMenuEventArgs : CancelEventArgs
- {
- public ContextMenu Menu { get; set; }
- }
- public class DynamicTabItemRenamedEventArgs : CancelEventArgs
- {
- public String OldName { get; set; }
- public String NewName { get; set; }
- }
- public delegate void DynamicTabControlChangedEvent(object sender, EventArgs args);
-
- public delegate void DynamicTabControlCreateEvent(object sender, DynamicTabControlEventArgs args);
-
- public delegate void DynamicTabItemCloseEvent(object sender, DynamicTabControlEventArgs args);
-
- public delegate void DynamicTabItemContextMenuEvent(object sender, DynamicTabItemContextMenuEventArgs args);
- public delegate void DynamicTabItemRenamedEvent(object sender, DynamicTabItemRenamedEventArgs args);
-
- public class DynamicTabControl : TabControl
- {
- public event DynamicTabControlChangedEvent OnTabsChanged;
-
- public static readonly DependencyProperty ChangedCommandProperty =
- DependencyProperty.Register(nameof(ChangedCommand), typeof(ICommand), typeof(DynamicTabControl));
- public ICommand ChangedCommand
- {
- get { return base.GetValue(ChangedCommandProperty) as ICommand; }
- set { base.SetValue(ChangedCommandProperty, value); }
- }
-
- public event DynamicTabControlCreateEvent OnCreateTab;
-
- public static readonly DependencyProperty CreateTabCommandProperty =
- DependencyProperty.Register(nameof(CreateTabCommand), typeof(ICommand), typeof(DynamicTabControl));
- public ICommand CreateTabCommand
- {
- get { return base.GetValue(CreateTabCommandProperty) as ICommand; }
- set { base.SetValue(CreateTabCommandProperty, value); }
- }
- public static readonly DependencyProperty CanCreateTabProperty =
- DependencyProperty.Register(nameof(CanCreateTab), typeof(bool), typeof(DynamicTabControl), new PropertyMetadata(false));
-
- public bool CanCreateTab
- {
- get => (bool)GetValue(CanCreateTabProperty);
- set => SetValue(CanCreateTabProperty, value);
- }
-
- static DynamicTabControl()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabControl), new FrameworkPropertyMetadata(typeof(DynamicTabControl)));
- }
- public DynamicTabControl() : base()
- {
- ChangedCommand = new ActionCommand(() => OnTabsChanged?.Invoke(this, new EventArgs()));
-
- CanCreateTab = false;
- CreateTabCommand = new ActionCommand(() =>
- {
- var args = new DynamicTabControlEventArgs() { TabItem = new DynamicTabItem() { Header = "New TabItem" }};
- OnCreateTab?.Invoke(this, args);
- if (args.Cancel == false)
- {
- Items.Add(args.TabItem);
- ChangedCommand.Execute(null);
- }
- });
- }
-
- protected override DependencyObject GetContainerForItemOverride()
- {
- return new DynamicTabItem();
- }
- }
-
- public class DynamicTabItem : TabItem
- {
- public event DynamicTabItemCloseEvent OnCloseTab;
-
- public static readonly DependencyProperty CloseTabCommandProperty =
- DependencyProperty.Register(nameof(CloseTabCommand), typeof(ICommand), typeof(DynamicTabItem));
- public ICommand CloseTabCommand
- {
- get { return base.GetValue(CloseTabCommandProperty) as ICommand; }
- set { base.SetValue(CloseTabCommandProperty, value); }
- }
-
- public static readonly DependencyProperty CanCloseProperty =
- DependencyProperty.Register(nameof(CanClose), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
-
- public bool CanClose
- {
- get => (bool)GetValue(CanCloseProperty);
- set => SetValue(CanCloseProperty, value);
- }
-
- public event DynamicTabItemContextMenuEvent OnContextMenuOpening;
- public static readonly DependencyProperty ContextMenuCommandProperty =
- DependencyProperty.Register(nameof(ContextMenuCommand), typeof(ICommand), typeof(DynamicTabItem));
- public ICommand ContextMenuCommand
- {
- get { return base.GetValue(ContextMenuCommandProperty) as ICommand; }
- set { base.SetValue(ContextMenuCommandProperty, value); }
- }
-
- public static readonly DependencyProperty CanRenameProperty =
- DependencyProperty.Register(nameof(CanRename), typeof(bool), typeof(DynamicTabItem), new PropertyMetadata(false));
-
- public bool CanRename
- {
- get => (bool)GetValue(CanRenameProperty);
- set => SetValue(CanRenameProperty, value);
- }
-
- public event DynamicTabItemRenamedEvent OnTabRenamed;
-
- static DynamicTabItem()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicTabItem), new FrameworkPropertyMetadata(typeof(DynamicTabItem)));
- }
- public DynamicTabItem() : base()
- {
- CloseTabCommand = new ActionCommand(() =>
- {
- bool bConfirm = false;
- if (OnCloseTab != null)
- {
- var args = new DynamicTabControlEventArgs() { TabItem = this };
- OnCloseTab?.Invoke(this, args);
- bConfirm = !args.Cancel;
- }
- else
- bConfirm = MessageBox.Show("Are you sure you wish to close this tab?", "Close Tab", MessageBoxButton.YesNo) ==
- MessageBoxResult.Yes;
-
- if (bConfirm)
- {
- var parent = Parent as DynamicTabControl;
- if (parent != null)
- {
- parent.Items.Remove(this);
- parent.ChangedCommand.Execute(null);
- }
- }
- });
- ContextMenuCommand = new ActionCommand(() =>
- {
- ContextMenu menu = new ContextMenu();
- if (CanRename)
- {
- menu.Items.Add(new MenuItem()
- {
- Header = "Rename Tab", Command = new ActionCommand(() =>
- {
- String tabname = Header.ToString() ?? string.Empty;
- if (TextEdit.Execute("Rename Tab", ref tabname))
- {
- OnTabRenamed?.Invoke(this, new DynamicTabItemRenamedEventArgs() { OldName = Header.ToString() ?? string.Empty, NewName = tabname });
- Header = tabname;
- var parent = Parent as DynamicTabControl;
- if (parent != null)
- parent.ChangedCommand.Execute(null);
- }
- })
- });
- }
- if (OnContextMenuOpening != null)
- {
- var args = new DynamicTabItemContextMenuEventArgs { Menu = menu };
- OnContextMenuOpening.Invoke(this, args);
- if (args.Cancel == false)
- args.Menu.IsOpen = true;
- }
- else
- menu.IsOpen = CanRename;
- });
- }
- }
-
- }
|