Binding a WPF ShortCut Key to a Command in the ViewModel

后端 未结 5 2194
我寻月下人不归
我寻月下人不归 2020-12-01 07:17

I have a WPF app that is using the MVVM pattern. Hooking up buttons to the VM is pretty straight forward since they implement the ICommand. I have a context menu that works

相关标签:
5条回答
  • 2020-12-01 07:54

    The following code can be used to bind a shortcut key directly to a command:

    <Window.InputBindings>
        <KeyBinding Command="{Binding Path=NameOfYourCommand}" 
                    Key="O" 
                    Modifiers="Control"/>
    </Window.InputBindings>
    

    Add this after Window.Resources in the XAML code of your view.

    0 讨论(0)
  • 2020-12-01 07:55

    An alternative approach for binding a WPF Shortcut Key to a Command property of the ViewModel is shown in the ShortcutKey sample of the WPF Application Framework (WAF) project.

    0 讨论(0)
  • 2020-12-01 07:59

    I agree that doing it in XAML is ideal, but for completeness sake, you could also add your binding in code. If you do it in the constructor, just make sure it is after the call to InitializeComponent()

    InputBindings.Add(new KeyBinding(btnUpdate, new KeyGesture(Key.U, ModifierKeys.Control));
    
    0 讨论(0)
  • 2020-12-01 08:03

    Have been able to add Keybinding on the DataGrid level. Like this :

    Xaml :

    <DataGrid 
                        AutoGenerateColumns="False"
                        ItemsSource="{Binding YourCollection}"                         
                        CanUserAddRows="False"                        
                        HeadersVisibility="Column" 
                        CanUserDeleteRows="False" 
                        CanUserSortColumns="True"
                        CanUserResizeRows="False"
                        CanUserResizeColumns="False"                       
                        SelectedItem="{Binding YourSelectedItem}" 
                        SelectionMode="Single" 
                        SelectionUnit="FullRow"
                       >
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                           **<MenuItem Header="Delete" InputGestureText="Del" Command="{Binding DeleteCommand}">**
                            </MenuItem>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                    **<DataGrid.InputBindings>
                        <KeyBinding Key="Delete" Command="{Binding DeleteCommand}" CommandParameter="Delete"/>**
                    </DataGrid.InputBindings>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Column Header" Binding="{Binding YourColumn}" IsReadOnly="True" />
                    </DataGrid.Columns>
    </DataGrid>
    

    View Model :

    public ICommand DeleteCommand
                {
                    get
                    {
                        return new DelegateCommand(ExecuteCommand, CanExecute);
                    }
                }
    
      private void ExecuteCommand()
    {
    // your code to delete here.
       YourCollection.Remove(YourSelectedItem);
    }
    
    private void CanExecute()
    {
    // logic to check if the delete command can execute.
       return YourSelectedItem != null ;
    }
    
    0 讨论(0)
  • 2020-12-01 08:17

    I wrote a custom markup extension to "bind" InputBindings to commands, which can be used almost like a real binding :

    <UserControl.InputBindings>
        <KeyBinding Modifiers="Control" 
                    Key="E" 
                    Command="{input:CommandBinding EditCommand}"/>
    </UserControl.InputBindings>
    

    Note that this markup extension uses private reflection, so it can only be used if your application runs in full trust...

    Another option is to use the CommandReference class. It can be found in the MVVM toolkit available here. It's probably a cleaner approach, but a bit more complex to use.

    Note that in WPF 4, the InputBinding.Command, InputBinding.CommandParameter and InputBinding.CommandTarget properties are dependency properties, so they can be bound normally

    0 讨论(0)
提交回复
热议问题