diff --git a/src/client/spark/view/Extensions/Command/AttachedCommands.cs b/src/client/spark/view/Extensions/Command/AttachedCommands.cs index 18819c357..175947ef8 100644 --- a/src/client/spark/view/Extensions/Command/AttachedCommands.cs +++ b/src/client/spark/view/Extensions/Command/AttachedCommands.cs @@ -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 { - 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); - } } } \ No newline at end of file diff --git a/src/client/spark/view/Extensions/WPFExtensions.cs b/src/client/spark/view/Extensions/WPFExtensions.cs index 8b00d008f..4b9af1e23 100644 --- a/src/client/spark/view/Extensions/WPFExtensions.cs +++ b/src/client/spark/view/Extensions/WPFExtensions.cs @@ -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);