fixed a bug of TabControl
This commit is contained in:
1 parent
fdadb2c2cb
commit
a8cb44e048
3 files changed
+51
-15
No files matched your search
@@ -94,8 +94,8 @@ namespace HandyControl.Controls
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ShowCloseButton
|
public bool ShowCloseButton
|
||||||
{
|
{
|
||||||
get => GetShowCloseButton(this);
|
get => (bool)GetValue(ShowCloseButtonProperty);
|
||||||
set => SetShowCloseButton(this, value);
|
set => SetValue(ShowCloseButtonProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -115,8 +115,8 @@ namespace HandyControl.Controls
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ShowContextMenu
|
public bool ShowContextMenu
|
||||||
{
|
{
|
||||||
get => GetShowContextMenu(this);
|
get => (bool)GetValue(ShowContextMenuProperty);
|
||||||
set => SetShowContextMenu(this, value);
|
set => SetValue(ShowContextMenuProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Animation;
|
using System.Windows.Media.Animation;
|
||||||
using HandyControl.Data;
|
using HandyControl.Data;
|
||||||
using HandyControl.Interactivity;
|
using HandyControl.Interactivity;
|
||||||
using HandyControl.Tools;
|
using HandyControl.Tools;
|
||||||
|
using HandyControl.Tools.Extension;
|
||||||
|
|
||||||
namespace HandyControl.Controls
|
namespace HandyControl.Controls
|
||||||
{
|
{
|
||||||
@@ -137,7 +139,18 @@ namespace HandyControl.Controls
|
|||||||
/// 是否显示上下文菜单
|
/// 是否显示上下文菜单
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly DependencyProperty ShowContextMenuProperty =
|
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>
|
/// <summary>
|
||||||
/// 是否显示上下文菜单
|
/// 是否显示上下文菜单
|
||||||
@@ -155,7 +168,30 @@ namespace HandyControl.Controls
|
|||||||
=> (bool)element.GetValue(ShowContextMenuProperty);
|
=> (bool)element.GetValue(ShowContextMenuProperty);
|
||||||
|
|
||||||
public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
|
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
|
public ContextMenu Menu
|
||||||
{
|
{
|
||||||
@@ -186,6 +222,8 @@ namespace HandyControl.Controls
|
|||||||
(s, e) => { TabControlParent.CloseAllItems(); }));
|
(s, e) => { TabControlParent.CloseAllItems(); }));
|
||||||
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther,
|
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther,
|
||||||
(s, e) => { TabControlParent.CloseOtherItems(this); }));
|
(s, e) => { TabControlParent.CloseOtherItems(this); }));
|
||||||
|
|
||||||
|
Loaded += (s, e) => OnMenuChanged(Menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TabControl TabControlParent => new Lazy<TabControl>(() => ItemsControl.ItemsControlFromItemContainer(this) as TabControl).Value;
|
private TabControl TabControlParent => new Lazy<TabControl>(() => ItemsControl.ItemsControlFromItemContainer(this) as TabControl).Value;
|
||||||
|
|||||||
@@ -13,6 +13,12 @@
|
|||||||
<ResourceDictionary Source="Base/TabControlBaseStyle.xaml"/>
|
<ResourceDictionary Source="Base/TabControlBaseStyle.xaml"/>
|
||||||
</ResourceDictionary.MergedDictionaries>
|
</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">
|
<Style TargetType="controls:TabItem">
|
||||||
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
||||||
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
|
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
|
||||||
@@ -23,15 +29,7 @@
|
|||||||
<Setter Property="Padding" Value="16,0"/>
|
<Setter Property="Padding" Value="16,0"/>
|
||||||
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
|
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
|
||||||
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
|
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
|
||||||
<Setter Property="Menu">
|
<Setter Property="Menu" Value="{StaticResource TabItemMenu}"/>
|
||||||
<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="HeaderTemplate">
|
<Setter Property="HeaderTemplate">
|
||||||
<Setter.Value>
|
<Setter.Value>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
|
|||||||
Reference in new issue
Block a user