You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace GummingCommon
{
public class InputBindingsBehavior
{
public static readonly DependencyProperty TakesInputBindingPrecedenceProperty =
DependencyProperty.RegisterAttached("TakesInputBindingPrecedence", typeof(bool), typeof(InputBindingsBehavior), new UIPropertyMetadata(false, OnTakesInputBindingPrecedenceChanged));
public static bool GetTakesInputBindingPrecedence(UIElement obj)
{
return (bool)obj.GetValue(TakesInputBindingPrecedenceProperty);
}
public static void SetTakesInputBindingPrecedence(UIElement obj, bool value)
{
obj.SetValue(TakesInputBindingPrecedenceProperty, value);
}
private static void OnTakesInputBindingPrecedenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((UIElement)d).PreviewKeyDown += new KeyEventHandler(InputBindingsBehavior_PreviewKeyDown);
}
private static void InputBindingsBehavior_PreviewKeyDown(object sender, KeyEventArgs e)
{
var uielement = (UIElement)sender;
var foundBinding = uielement.InputBindings
.OfType<KeyBinding>()
.FirstOrDefault(kb => kb.Key == e.Key && kb.Modifiers == e.KeyboardDevice.Modifiers);
if (foundBinding != null)
{
e.Handled = true;
if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
{
foundBinding.Command.Execute(foundBinding.CommandParameter);
}
}
}
}
}