Skip to content

Commit

Permalink
fixed a bug of TabControl
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Nov 2, 2019
1 parent fdadb2c commit a8cb44e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public static bool GetShowCloseButton(DependencyObject element)
/// </summary>
public bool ShowCloseButton
{
get => GetShowCloseButton(this);
set => SetShowCloseButton(this, value);
get => (bool)GetValue(ShowCloseButtonProperty);
set => SetValue(ShowCloseButtonProperty, value);
}

/// <summary>
Expand All @@ -115,8 +115,8 @@ public static bool GetShowContextMenu(DependencyObject element)
/// </summary>
public bool ShowContextMenu
{
get => GetShowContextMenu(this);
set => SetShowContextMenu(this, value);
get => (bool)GetValue(ShowContextMenuProperty);
set => SetValue(ShowContextMenuProperty, value);
}

/// <summary>
Expand Down
42 changes: 40 additions & 2 deletions src/Shared/HandyControl_Shared/Controls/TabControl/TabItem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools;
using HandyControl.Tools.Extension;

namespace HandyControl.Controls
{
Expand Down Expand Up @@ -137,7 +139,18 @@ public static bool GetShowCloseButton(DependencyObject element)
/// 是否显示上下文菜单
/// </summary>
public static readonly DependencyProperty ShowContextMenuProperty =
TabControl.ShowContextMenuProperty.AddOwner(typeof(TabItem));
TabControl.ShowContextMenuProperty.AddOwner(typeof(TabItem), new FrameworkPropertyMetadata(OnShowContextMenuChanged));

private static void OnShowContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (TabItem) d;
if (ctl.Menu != null)
{
var show = (bool)e.NewValue;
ctl.Menu.IsEnabled = show;
ctl.Menu.Show(show);
}
}

/// <summary>
/// 是否显示上下文菜单
Expand All @@ -155,7 +168,30 @@ public static bool GetShowContextMenu(DependencyObject element)
=> (bool)element.GetValue(ShowContextMenuProperty);

public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
"Menu", typeof(ContextMenu), typeof(TabItem), new PropertyMetadata(default(ContextMenu)));
"Menu", typeof(ContextMenu), typeof(TabItem), new PropertyMetadata(default(ContextMenu), OnMenuChanged));

private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (TabItem) d;
ctl.OnMenuChanged(e.NewValue as ContextMenu);
}

private void OnMenuChanged(ContextMenu menu)
{
if (IsLoaded && menu != null)
{
menu.DataContext = this;
menu.SetBinding(IsEnabledProperty, new Binding(ShowContextMenuProperty.Name)
{
Source = this
});
menu.SetBinding(VisibilityProperty, new Binding(ShowContextMenuProperty.Name)
{
Source = this,
Converter = ResourceHelper.GetResource<IValueConverter>("Boolean2VisibilityConverter")
});
}
}

public ContextMenu Menu
{
Expand Down Expand Up @@ -186,6 +222,8 @@ public TabItem()
(s, e) => { TabControlParent.CloseAllItems(); }));
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther,
(s, e) => { TabControlParent.CloseOtherItems(this); }));

Loaded += (s, e) => OnMenuChanged(Menu);
}

private TabControl TabControlParent => new Lazy<TabControl>(() => ItemsControl.ItemsControlFromItemContainer(this) as TabControl).Value;
Expand Down
16 changes: 7 additions & 9 deletions src/Shared/HandyControl_Shared/Themes/Styles/TabControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<ResourceDictionary Source="Base/TabControlBaseStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

<ContextMenu x:Key="TabItemMenu" x:Shared="False">
<MenuItem Command="interactivity:ControlCommands.Close" Header="{x:Static langs:Lang.Close}"/>
<MenuItem Command="interactivity:ControlCommands.CloseAll" Header="{x:Static langs:Lang.CloseAll}"/>
<MenuItem Command="interactivity:ControlCommands.CloseOther" Header="{x:Static langs:Lang.CloseOther}"/>
</ContextMenu>

<Style TargetType="controls:TabItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
Expand All @@ -23,15 +29,7 @@
<Setter Property="Padding" Value="16,0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Menu">
<Setter.Value>
<ContextMenu IsEnabled="{Binding ShowContextMenu,RelativeSource={RelativeSource AncestorType=controls:TabItem}}" Visibility="{Binding ShowContextMenu,RelativeSource={RelativeSource AncestorType=controls:TabItem},Converter={StaticResource Boolean2VisibilityConverter}}">
<MenuItem Command="interactivity:ControlCommands.Close" Header="{x:Static langs:Lang.Close}"/>
<MenuItem Command="interactivity:ControlCommands.CloseAll" Header="{x:Static langs:Lang.CloseAll}"/>
<MenuItem Command="interactivity:ControlCommands.CloseOther" Header="{x:Static langs:Lang.CloseOther}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Menu" Value="{StaticResource TabItemMenu}"/>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
Expand Down

0 comments on commit a8cb44e

Please sign in to comment.