fixed a bug of TabControl

This commit is contained in:
NaBian committed 2019-11-02 15:00:02 +08:00
1 parent fdadb2c2cb
commit a8cb44e048
3 files changed
+51 -15

No files matched your search

@@ -94,8 +94,8 @@ namespace HandyControl.Controls
/// </summary>
public bool ShowCloseButton
{
get => GetShowCloseButton(this);
set => SetShowCloseButton(this, value);
get => (bool)GetValue(ShowCloseButtonProperty);
set => SetValue(ShowCloseButtonProperty, value);
}
/// <summary>
@@ -115,8 +115,8 @@ namespace HandyControl.Controls
/// </summary>
public bool ShowContextMenu
{
get => GetShowContextMenu(this);
set => SetShowContextMenu(this, value);
get => (bool)GetValue(ShowContextMenuProperty);
set => SetValue(ShowContextMenuProperty, value);
}
/// <summary>
@@ -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
{
@@ -137,7 +139,18 @@ namespace HandyControl.Controls
/// 是否显示上下文菜单
/// </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>
/// 是否显示上下文菜单
@@ -155,7 +168,30 @@ namespace HandyControl.Controls
=> (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
{
@@ -186,6 +222,8 @@ namespace HandyControl.Controls
(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;
@@ -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}"/>
@@ -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>