public CustomKeyGestureExtension(string gestureString)
        {
            // TODO: Implement custom parsing logic, because Converter also invokes
            // validating KeyGesture contructor.
            var original = (KeyGesture)KeyGestureConverter.ConvertFromString(gestureString);

            _keyGesture = CustomKeyGesture.Create(original.Key, original.Modifiers);
        }
示例#2
0
        public static IOptionsExpression <T> KeyGesture <T>(
            this IOptionsExpression <T> expression,
            Key key,
            ModifierKeys modifiers = ModifierKeys.None
            ) where T : ICommand
        {
            KeyGesture   gesture = CustomKeyGesture.Create(key, modifiers);
            InputBinding binding = new KeyBinding {
                Gesture = gesture
            };

            BinderExpression.ExposeContext(
                expression,
                ctx => {
                BindingOperations.SetBinding(binding, KeyBinding.CommandProperty, ctx.Binding);

                var targetUIElement      = ctx.TargetObject as UIElement;
                var targetContentElement = ctx.TargetObject as ContentElement;

                if (targetUIElement != null)
                {
                    targetUIElement.InputBindings.Add(binding);
                }
                else if (targetContentElement != null)
                {
                    targetContentElement.InputBindings.Add(binding);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
                );

            return(expression);
        }