添加项目文件。
This commit is contained in:
54
UIShare/Behaviors/MouseDoubleClickBehavior.cs
Normal file
54
UIShare/Behaviors/MouseDoubleClickBehavior.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace UIShare.Behaviors
|
||||
{
|
||||
//给Border添加双击事件
|
||||
public class MouseDoubleClickBehavior : Behavior<Border>
|
||||
{
|
||||
public static readonly DependencyProperty CommandProperty =
|
||||
DependencyProperty.Register("Command", typeof(ICommand), typeof(MouseDoubleClickBehavior), new PropertyMetadata(null));
|
||||
|
||||
public ICommand Command
|
||||
{
|
||||
get => (ICommand)GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty CommandParameterProperty =
|
||||
DependencyProperty.Register("CommandParameter", typeof(object), typeof(MouseDoubleClickBehavior), new PropertyMetadata(null));
|
||||
|
||||
public object CommandParameter
|
||||
{
|
||||
get => GetValue(CommandParameterProperty);
|
||||
set => SetValue(CommandParameterProperty, value);
|
||||
}
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
// 确保 Border 的 Background 不为 null,否则无法触发点击事件
|
||||
this.AssociatedObject.MouseLeftButtonDown += OnMouseLeftButtonDown;
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
base.OnDetaching();
|
||||
this.AssociatedObject.MouseLeftButtonDown -= OnMouseLeftButtonDown;
|
||||
}
|
||||
|
||||
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
// 判断点击次数是否为 2
|
||||
if (e.ClickCount == 2)
|
||||
{
|
||||
if (Command != null && Command.CanExecute(CommandParameter))
|
||||
{
|
||||
Command.Execute(CommandParameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
UIShare/Behaviors/TabControlSelectionChangedBehavior.cs
Normal file
59
UIShare/Behaviors/TabControlSelectionChangedBehavior.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace UIShare.Behaviors
|
||||
{
|
||||
public class TabControlSelectionChangedBehavior : Behavior<TabControl>
|
||||
{
|
||||
public static readonly DependencyProperty CommandProperty =
|
||||
DependencyProperty.Register(
|
||||
"Command", typeof(ICommand), typeof(TabControlSelectionChangedBehavior), new PropertyMetadata(null));
|
||||
|
||||
public static readonly DependencyProperty CommandParameterProperty =
|
||||
DependencyProperty.Register(
|
||||
"CommandParameter", typeof(object), typeof(TabControlSelectionChangedBehavior), new PropertyMetadata(null));
|
||||
|
||||
public ICommand Command
|
||||
{
|
||||
get { return (ICommand)GetValue(CommandProperty); }
|
||||
set { SetValue(CommandProperty, value); }
|
||||
}
|
||||
|
||||
public object CommandParameter
|
||||
{
|
||||
get { return GetValue(CommandParameterProperty); }
|
||||
set { SetValue(CommandParameterProperty, value); }
|
||||
}
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
if (AssociatedObject != null)
|
||||
{
|
||||
AssociatedObject.SelectionChanged += OnSelectionChanged;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
if (AssociatedObject != null)
|
||||
{
|
||||
AssociatedObject.SelectionChanged -= OnSelectionChanged;
|
||||
}
|
||||
base.OnDetaching();
|
||||
}
|
||||
|
||||
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
var tabItem = AssociatedObject.SelectedItem as TabItem;
|
||||
// 获取选中 TabItem 的 Header
|
||||
if (Command != null && Command.CanExecute(tabItem.Header))
|
||||
{
|
||||
// 使用选中 TabItem 的 Header 作为 CommandParameter
|
||||
Command.Execute(((TabItem)AssociatedObject.SelectedItem)?.Header);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user