AttachedCommands.DoubleClick uses InputBinding rather than DoubleClick event
This commit is contained in:
1 parent
719703b6e3
commit
3d4d0fc75d
2 files changed
+23
-43
No files matched your search
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
|
||||
@@ -10,12 +8,14 @@ namespace mbts.ui
|
||||
{
|
||||
#region DoubleClick
|
||||
public static readonly DependencyProperty DoubleClickProperty = DependencyProperty.RegisterAttached("DoubleClick", typeof(ICommand), typeof(AttachedCommands), new PropertyMetadata(OnDoubleClickChanged));
|
||||
private static void OnDoubleClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
||||
var command = e.NewValue as ICommand;
|
||||
if (command == null) SetDoubleClickDependency(d, null);
|
||||
else {
|
||||
var target = (Control)d;
|
||||
SetDoubleClickDependency(target, new DoubleClickLifeDependency(target, command));
|
||||
static void OnDoubleClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
||||
var el = (UIElement)d;
|
||||
el.ClearMouseBindings(MouseAction.LeftDoubleClick);
|
||||
var cmd = e.NewValue as ICommand;
|
||||
if (cmd != null) {
|
||||
var mb = new MouseBinding(cmd, new MouseGesture(MouseAction.LeftDoubleClick));
|
||||
BindingOperations.SetBinding(mb, InputBinding.CommandParameterProperty, new Binding { Path = new PropertyPath(DoubleClickParameterProperty), Source = d });
|
||||
el.InputBindings.Add(mb);
|
||||
}
|
||||
}
|
||||
public static ICommand GetDoubleClick(DependencyObject target) { return target.GetValue(DoubleClickProperty) as ICommand; }
|
||||
@@ -28,14 +28,6 @@ namespace mbts.ui
|
||||
public static void SetDoubleClickParameter(DependencyObject target, object value) { target.SetValue(DoubleClickParameterProperty, value); }
|
||||
#endregion
|
||||
|
||||
#region DoubleClickDependency
|
||||
private static readonly DependencyPropertyKey DoubleClickDependencyPropertyKey = DependencyProperty.RegisterAttachedReadOnly("DoubleClickDependency", typeof(IDisposable), typeof(AttachedCommands),
|
||||
new PropertyMetadata(FrameworkElementLifeDependencyHelper.DisposeOldInitializeNew));
|
||||
internal static DependencyProperty DoubleClickDependencyProperty = DoubleClickDependencyPropertyKey.DependencyProperty;
|
||||
internal static IDisposable GetDoubleClickDependency(DependencyObject target) => target.GetValue(DoubleClickDependencyProperty) as IDisposable;
|
||||
private static void SetDoubleClickDependency(DependencyObject target, IDisposable value) => target.SetValue(DoubleClickDependencyPropertyKey, value);
|
||||
#endregion
|
||||
|
||||
#region Enter
|
||||
public static readonly DependencyProperty EnterProperty = DependencyProperty.RegisterAttached("Enter", typeof(ICommand), typeof(AttachedCommands), new PropertyMetadata(OnEnterChanged));
|
||||
static void OnEnterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
||||
@@ -57,31 +49,5 @@ namespace mbts.ui
|
||||
public static object GetEnterParameter(DependencyObject d) => d.GetValue(EnterParameterProperty);
|
||||
public static void SetEnterParameter(DependencyObject d, object value) => d.SetValue(EnterParameterProperty, value);
|
||||
#endregion
|
||||
|
||||
|
||||
private class DoubleClickLifeDependency : FrameworkElementLifeDependency<Control> {
|
||||
public DoubleClickLifeDependency(Control target, ICommand command) : base(target) =>
|
||||
_command = command;
|
||||
|
||||
readonly ICommand _command;
|
||||
IDisposable _hook;
|
||||
|
||||
void _set_hook() {
|
||||
Disposable.dispose(ref _hook);
|
||||
Target.MouseDoubleClick += OnMouseDoubleClick;
|
||||
_hook = Disposable.Create(() => Target.MouseDoubleClick -= OnMouseDoubleClick);
|
||||
}
|
||||
|
||||
void OnMouseDoubleClick(object sender, MouseButtonEventArgs e) {
|
||||
object param = GetDoubleClickParameter(Target);
|
||||
if (_command.CanExecute(param))
|
||||
_command.Execute(param);
|
||||
}
|
||||
|
||||
protected override void OnInitialized() => _set_hook();
|
||||
protected override void OnLoaded() => _set_hook();
|
||||
protected override void OnUnloaded() => Disposable.dispose(ref _hook);
|
||||
protected override void OnDisposed() => Disposable.dispose(ref _hook);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,6 +146,20 @@ namespace mbts.ui
|
||||
});
|
||||
}
|
||||
|
||||
public static void ClearMouseBindings(this UIElement element, MouseAction action) =>
|
||||
ClearMouseBindings(element, action, ModifierKeys.None);
|
||||
|
||||
public static void ClearMouseBindings(this UIElement element, MouseAction action, ModifierKeys modifiers) {
|
||||
for (int i = 0; i < element.InputBindings.Count; i++) {
|
||||
var mb = element.InputBindings[i] as MouseBinding;
|
||||
if (mb != null) {
|
||||
var g = mb.Gesture as MouseGesture;
|
||||
if (g != null && g.MouseAction == action && g.Modifiers == modifiers)
|
||||
element.InputBindings.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearKeyBindings(this UIElement element, Key key) =>
|
||||
ClearKeyBindings(element, key, ModifierKeys.None);
|
||||
|
||||
|
||||
Reference in new issue
Block a user