136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
namespace Melskin.Controls;
|
|
/// <summary>
|
|
/// 表示一个可关闭的标签控件。
|
|
/// </summary>
|
|
[TemplatePart(Name = PART_Close, Type = typeof(UIElement))]
|
|
public class Tag : System.Windows.Controls.ContentControl
|
|
{
|
|
#region Fileds
|
|
private const string PART_Close = "PART_Close";
|
|
|
|
private UIElement? close;
|
|
#endregion
|
|
|
|
#region Events
|
|
|
|
/// <summary>
|
|
/// 当标签即将关闭时触发的路由事件。
|
|
/// </summary>
|
|
public static readonly RoutedEvent ClosingEvent =
|
|
EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Tag));
|
|
|
|
/// <summary>
|
|
/// Occurs when closing the tag.
|
|
/// </summary>
|
|
public event RoutedEventHandler Closing
|
|
{
|
|
add => AddHandler(ClosingEvent, value);
|
|
remove => RemoveHandler(ClosingEvent, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当标签关闭后触发的路由事件。
|
|
/// </summary>
|
|
public static readonly RoutedEvent ClosedEvent =
|
|
EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Tag));
|
|
|
|
/// <summary>
|
|
/// Occurs when a Tag is closed and is no longer visible.
|
|
/// </summary>
|
|
public event RoutedEventHandler Closed
|
|
{
|
|
add => AddHandler(ClosedEvent, value);
|
|
remove => RemoveHandler(ClosedEvent, value);
|
|
}
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// 表示标签是否可以关闭的依赖属性。
|
|
/// </summary>
|
|
public static readonly DependencyProperty ClosableProperty =
|
|
DependencyProperty.Register(
|
|
nameof(Closable),
|
|
typeof(bool),
|
|
typeof(Tag),
|
|
new PropertyMetadata(false/*, OnClosableChnaged*/));
|
|
|
|
/// <summary>
|
|
/// Gets/sets whether the Tag can be closed.
|
|
/// </summary>
|
|
public bool Closable
|
|
{
|
|
get => (bool)GetValue(ClosableProperty);
|
|
set => SetValue(ClosableProperty, value);
|
|
}
|
|
|
|
//private static void OnClosableChnaged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
//{
|
|
// (d as Tag).SetCloseVisibility();
|
|
//}
|
|
|
|
//private void SetCloseVisibility()
|
|
//{
|
|
// if (close != null)
|
|
// {
|
|
// close.Visibility = Closable ? Visibility.Visible : Visibility.Collapsed;
|
|
// }
|
|
//}
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
static Tag() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Tag), new FrameworkPropertyMetadata(typeof(Tag))); }
|
|
#endregion
|
|
|
|
#region Overrides
|
|
|
|
/// <inheritdoc />
|
|
public override void OnApplyTemplate()
|
|
{
|
|
base.OnApplyTemplate();
|
|
|
|
if (close != null)
|
|
{
|
|
close.MouseLeftButtonUp -= OnRaiseClosingEvent;
|
|
close.PreviewMouseLeftButtonDown -= Close_PreviewMouseLeftButtonDown;
|
|
}
|
|
|
|
close = GetTemplateChild(PART_Close) as UIElement;
|
|
|
|
if (close != null)
|
|
{
|
|
Loaded -= OnLoaded;
|
|
Loaded += OnLoaded;
|
|
|
|
close.PreviewMouseLeftButtonDown += Close_PreviewMouseLeftButtonDown; //阻止ComboBox的ToggleButton切换
|
|
close.MouseLeftButtonUp += OnRaiseClosingEvent;
|
|
}
|
|
}
|
|
|
|
private void Close_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
|
|
{
|
|
// 阻止事件继续向上隧道到 ToggleButton
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Closing -= OnClosing;
|
|
Closing += OnClosing;
|
|
}
|
|
|
|
private void OnRaiseClosingEvent(object sender, RoutedEventArgs e)
|
|
{
|
|
e.Handled = true;
|
|
RaiseEvent(new RoutedEventArgs(ClosingEvent, this));
|
|
}
|
|
|
|
private void OnClosing(object sender, RoutedEventArgs e)
|
|
{
|
|
SetCurrentValue(VisibilityProperty, Visibility.Collapsed);
|
|
RaiseEvent(new RoutedEventArgs(ClosedEvent, this));
|
|
}
|
|
#endregion
|
|
} |