Apply Command InputBindings on styles

This one is a little bit more tricky then the CommandProxy I’ve written before, because the CommandProxy dose not work here.

First you can simply not apply InputBindings from Styles because the property has not setter. You are only able to add bindings to the collection, but there is no way to do this in styles.
Second you can not place there resource for the CommandProxy inside of the style.

To make that work you need an property which you can set in the style. And this is my solution. Create an attached dependency property which set the InputBinding for you.

As example i use the LeftDoubleClick MouseGesture.

public static class MouseCommands
    private static void LeftDoubleClickChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        var control = (Control)sender;

        if(args.NewValue != null && args.NewValue is ICommand)
            var newBinding = new MouseBinding(args.NewValue as ICommand, new MouseGesture(MouseAction.LeftDoubleClick));
            var oldBinding = control.InputBindings.Cast<InputBinding>().First(b => b.Command.Equals(args.OldValue));

    public static readonly DependencyProperty LeftDoubleClickProperty =
            new UIPropertyMetadata(LeftDoubleClickChanged));

    public static void SetLeftDoubleClick(DependencyObject obj, ICommand value)
        obj.SetValue(LeftDoubleClickProperty, value);

    public static ICommand GetLeftDoubleClick(DependencyObject obj)
        return (ICommand)obj.GetValue(LeftDoubleClickProperty);

And then you can apply this over a style. In my example here for an ListBoxItem in its ListBoxContainerStyle.

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Shared:MouseCommands.LeftDoubleClick" Value="{Binding AddCommand}" />

And that it is. Yes you have to create an attached dependency property for each Mouse- or KeyBinding you want to use, but the most time you dose not need all of them.