From 0a2afbaadd94308a916cb36b5e93405c368866b5 Mon Sep 17 00:00:00 2001 From: Amrykid Date: Sat, 8 Jun 2013 07:41:44 -0500 Subject: [PATCH 1/7] CloseTabCommand should work /better/. All TabControls inherit from BaseMetroTabControl in order to make extending the TabControls easier. --- .../MetroAnimatedSingleRowTabControl.cs | 27 +-------- .../Controls/MetroAnimatedTabControl.cs | 27 +-------- MahApps.Metro/Controls/MetroTabControl.cs | 55 ++++++++++++++++--- MahApps.Metro/Themes/MetroTabItem.xaml | 4 +- 4 files changed, 52 insertions(+), 61 deletions(-) diff --git a/MahApps.Metro/Controls/MetroAnimatedSingleRowTabControl.cs b/MahApps.Metro/Controls/MetroAnimatedSingleRowTabControl.cs index 27895ccdad..cdb1ed831f 100644 --- a/MahApps.Metro/Controls/MetroAnimatedSingleRowTabControl.cs +++ b/MahApps.Metro/Controls/MetroAnimatedSingleRowTabControl.cs @@ -1,38 +1,15 @@ using System.Windows; using System.Windows.Controls; +using System.Windows.Data; using System.Windows.Input; namespace MahApps.Metro.Controls { - public class MetroAnimatedSingleRowTabControl : TabControl + public class MetroAnimatedSingleRowTabControl : BaseMetroTabControl { public MetroAnimatedSingleRowTabControl() { DefaultStyleKey = typeof(MetroAnimatedSingleRowTabControl); } - - public Thickness TabStripMargin - { - get { return (Thickness)GetValue(TabStripMarginProperty); } - set { SetValue(TabStripMarginProperty, value); } - } - - // Using a DependencyProperty as the backing store for TabStripMargin. This enables animation, styling, binding, etc... - public static readonly DependencyProperty TabStripMarginProperty = - DependencyProperty.Register("TabStripMargin", typeof(Thickness), typeof(MetroAnimatedSingleRowTabControl), new PropertyMetadata(new Thickness(0))); - - protected override DependencyObject GetContainerForItemOverride() - { - return new MetroTabItem() { OwningTabControl = this }; //Overrides the TabControl's default behavior and returns a MetroTabItem instead of a regular one. - } - - public ICommand CloseTabCommand - { - get { return (ICommand)GetValue(CloseTabCommandProperty); } - set { SetValue(CloseTabCommandProperty, value); } - } - - public static readonly DependencyProperty CloseTabCommandProperty = - DependencyProperty.Register("CloseTabCommand", typeof(ICommand), typeof(MetroAnimatedSingleRowTabControl), new PropertyMetadata(new MahApps.Metro.Controls.MetroTabControl.DefaultCloseTabCommand())); } } diff --git a/MahApps.Metro/Controls/MetroAnimatedTabControl.cs b/MahApps.Metro/Controls/MetroAnimatedTabControl.cs index 03c33f2897..ffc2aae1b7 100644 --- a/MahApps.Metro/Controls/MetroAnimatedTabControl.cs +++ b/MahApps.Metro/Controls/MetroAnimatedTabControl.cs @@ -1,38 +1,15 @@ using System.Windows; using System.Windows.Controls; +using System.Windows.Data; using System.Windows.Input; namespace MahApps.Metro.Controls { - public class MetroAnimatedTabControl : TabControl + public class MetroAnimatedTabControl : BaseMetroTabControl { public MetroAnimatedTabControl() { DefaultStyleKey = typeof(MetroAnimatedTabControl); } - - public Thickness TabStripMargin - { - get { return (Thickness)GetValue(TabStripMarginProperty); } - set { SetValue(TabStripMarginProperty, value); } - } - - // Using a DependencyProperty as the backing store for TabStripMargin. This enables animation, styling, binding, etc... - public static readonly DependencyProperty TabStripMarginProperty = - DependencyProperty.Register("TabStripMargin", typeof(Thickness), typeof(MetroAnimatedTabControl), new PropertyMetadata(new Thickness(0))); - - protected override DependencyObject GetContainerForItemOverride() - { - return new MetroTabItem() { OwningTabControl = this }; //Overrides the TabControl's default behavior and returns a MetroTabItem instead of a regular one. - } - - public ICommand CloseTabCommand - { - get { return (ICommand)GetValue(CloseTabCommandProperty); } - set { SetValue(CloseTabCommandProperty, value); } - } - - public static readonly DependencyProperty CloseTabCommandProperty = - DependencyProperty.Register("CloseTabCommand", typeof(ICommand), typeof(MetroAnimatedTabControl), new PropertyMetadata(new MahApps.Metro.Controls.MetroTabControl.DefaultCloseTabCommand())); } } diff --git a/MahApps.Metro/Controls/MetroTabControl.cs b/MahApps.Metro/Controls/MetroTabControl.cs index d7ef90fde8..793c31cea7 100644 --- a/MahApps.Metro/Controls/MetroTabControl.cs +++ b/MahApps.Metro/Controls/MetroTabControl.cs @@ -1,14 +1,23 @@ using System.Windows; using System.Windows.Controls; +using System.Windows.Data; using System.Windows.Input; namespace MahApps.Metro.Controls { - public class MetroTabControl : TabControl + public class MetroTabControl : BaseMetroTabControl { - public MetroTabControl() + public MetroTabControl():base() { - DefaultStyleKey = typeof(MetroTabControl); + DefaultStyleKey = typeof(MetroTabControl); + } + } + + public abstract class BaseMetroTabControl: TabControl + { + public BaseMetroTabControl() + { + InternalCloseTabCommand = new DefaultCloseTabCommand(this); } public Thickness TabStripMargin @@ -19,13 +28,19 @@ public Thickness TabStripMargin // Using a DependencyProperty as the backing store for TabStripMargin. This enables animation, styling, binding, etc... public static readonly DependencyProperty TabStripMarginProperty = - DependencyProperty.Register("TabStripMargin", typeof(Thickness), typeof(MetroTabControl), new PropertyMetadata(new Thickness(0))); + DependencyProperty.Register("TabStripMargin", typeof(Thickness), typeof(BaseMetroTabControl), new PropertyMetadata(new Thickness(0))); protected override DependencyObject GetContainerForItemOverride() { return new MetroTabItem() { OwningTabControl = this }; //Overrides the TabControl's default behavior and returns a MetroTabItem instead of a regular one. } + protected override void PrepareContainerForItemOverride(DependencyObject element, object item) + { + element.SetCurrentValue(MetroTabItem.DataContextProperty, item); + base.PrepareContainerForItemOverride(element, item); + } + public ICommand CloseTabCommand { get { return (ICommand)GetValue(CloseTabCommandProperty); } @@ -33,10 +48,25 @@ public ICommand CloseTabCommand } public static readonly DependencyProperty CloseTabCommandProperty = - DependencyProperty.Register("CloseTabCommand", typeof(ICommand), typeof(MetroTabControl), new PropertyMetadata(new DefaultCloseTabCommand())); + DependencyProperty.Register("CloseTabCommand", typeof(ICommand), typeof(BaseMetroTabControl), new PropertyMetadata(null)); + + private ICommand InternalCloseTabCommand + { + get { return (ICommand)GetValue(InternalCloseTabCommandProperty); } + set { SetValue(InternalCloseTabCommandProperty, value); } + } + private static readonly DependencyProperty InternalCloseTabCommandProperty = + DependencyProperty.Register("InternalCloseTabCommand", typeof(ICommand), typeof(BaseMetroTabControl), new PropertyMetadata(null)); + internal class DefaultCloseTabCommand : ICommand { + private BaseMetroTabControl owner = null; + internal DefaultCloseTabCommand(BaseMetroTabControl Owner) + { + owner = Owner; + } + public bool CanExecute(object parameter) { return true; @@ -46,11 +76,18 @@ public bool CanExecute(object parameter) public void Execute(object parameter) { - if (parameter != null && parameter is MetroTabItem) + if (parameter != null) { - var tabItem = (MetroTabItem)parameter; - - ((TabControl)tabItem.OwningTabControl).Items.Remove(tabItem); + if (owner.CloseTabCommand != null) + owner.CloseTabCommand.Execute(parameter); + else + { + if (parameter is MetroTabItem) + { + var tabItem = (MetroTabItem)parameter; + owner.Items.Remove(tabItem); + } + } } } } diff --git a/MahApps.Metro/Themes/MetroTabItem.xaml b/MahApps.Metro/Themes/MetroTabItem.xaml index 7e2b7ef498..0f6d0599b2 100644 --- a/MahApps.Metro/Themes/MetroTabItem.xaml +++ b/MahApps.Metro/Themes/MetroTabItem.xaml @@ -23,8 +23,8 @@