1

In my WPF MVVM application each model-view contains a list of buttons. Which are valid for that user control.

private List<myButton> _buttons;

I am displaying them like this:

 <ItemsControl ItemsSource="{Binding buttons}" >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>                    
                    <Button   Width="100" Height="40" VerticalAlignment="Top" Margin="5,5,5,5" Command="{Binding command}"  Content="{Binding name}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Which works just fine.

Now what i would like to do is have key bindings on some of my buttons Save for example cntr + s.

How do I add key bindings for some of the buttons. From what i have found so far you would do something like this

<Window.InputBindings>
    <KeyBinding Key="Z" Modifiers="Ctrl" Command="{StaticResource MyCommand1}" />
    <KeyBinding Key="H" Modifiers="Alt" Command="{StaticResource MyCommand2}" />
</Window.InputBindings>

I tried adding it as a list but that didnt work at all. There must be a way of bulding the key bindings for the some of my buttons.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    http://stackoverflow.com/questions/1361350/keyboard-shortcuts-in-wpf – bwegs Apr 20 '16 at 12:55
  • Sounds like you are thinking about [binding InputBindings](http://stackoverflow.com/q/9385129/1997232). – Sinatr Apr 20 '16 at 12:58
  • @bwegs I tried that one already it doesn't work because buttons is a list. I need to be able to loop somehow creating the different key bindings. – Linda Lawton - DaImTo Apr 20 '16 at 13:00
  • @DaImTo same problem here... still no one has answer to help ! can you also double-check my thread? - http://stackoverflow.com/questions/36423870/c-sharp-keyboard-shortcuts-quit-working-after-calling-2nd-xaml-window – 16per9 Apr 20 '16 at 13:03
  • I also use WPF with XAML windows... and they just stop working. But I am able to make the bindings – 16per9 Apr 20 '16 at 13:04
  • @16per9 not a bad idea but that will require that I add that to all of the user controls I am trying to keep code out of the code behind. – Linda Lawton - DaImTo Apr 20 '16 at 13:06

1 Answers1

1

I got it!

The thing is that Inputbindings are a window thing not a user control thing. Yes I am new at WPF.

By adding the following to the Mainwindow.xml it will fire savecommand on which ever usercontrol is currently selected.

<Window.InputBindings>
    <KeyBinding  Key="s" Modifiers="Ctrl" Command="{Binding ElementName=ListBoxMenu, Path=SelectedItem.SaveCommand }" />
</Window.InputBindings>

If the usercontrol / modelview what ever its called doesn't have a savecommand it just does nothing.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449