Create Key binding in WPF

后端 未结 5 1863
滥情空心
滥情空心 2020-11-30 08:47

I need to create input binding for Window.

public class MainWindow : Window
{
    public MainWindow()
    {
        SomeCommand = ??? () => OnAction();
           


        
相关标签:
5条回答
  • 2020-11-30 09:10

    It might be too late but here is the simplest and shortest solution.

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S)
        {
             // Call your method here
        }
    }
    
    <Window x:Class="Test.MainWindow" KeyDown="Window_KeyDown" >
    
    0 讨论(0)
  • 2020-11-30 09:13

    For your case best way used MVVM pattern

    XAML:

    <Window>
        <Window.InputBindings>
            <KeyBinding Command="{Binding SomeCommand}" Key="F5"/>
        </Window.InputBindings>
    </Window>
    

    Code behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    

    In your view-model:

    public class MyViewModel
    {
        private ICommand someCommand;
        public ICommand SomeCommand
        {
            get
            {
                return someCommand 
                    ?? (someCommand = new ActionCommand(() =>
                    {
                        MessageBox.Show("SomeCommand");
                    }));
            }
        }
    }
    

    Then you'll need an implementation of ICommand. This simple helpful class.

    public class ActionCommand : ICommand
    {
        private readonly Action _action;
    
        public ActionCommand(Action action)
        {
            _action = action;
        }
    
        public void Execute(object parameter)
        {
            _action();
        }
    
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public event EventHandler CanExecuteChanged;
    }   
    
    0 讨论(0)
  • 2020-11-30 09:17

    This is how I solved this problem in my project:

    <Window x:Class="MyNamespace.MyView"
        (...)
        xmlns:local="clr-namespace:MyNameSpace"
        (...)
        <Grid>
            <Grid.InputBindings>
                <KeyBinding Key="R" Command="{Binding ReportCommand, 
                    RelativeSource={RelativeSource AncestorType=local:MyView}}" />
        (...)
    

    ReportCommand is an ICommand in MyView, not in the ViewModel.

    0 讨论(0)
  • 2020-11-30 09:18

    For modifiers (key combinations):

    <KeyBinding Command="{Binding SaveCommand}" Modifiers="Control" Key="S"/>
    
    0 讨论(0)
  • 2020-11-30 09:25

    You will have to create your own Command implementing ICommand interface and initialize SomeCommand with the instance of that Command.

    Now you have to set the DataContext of Window to self in order to make the Command Binding work:

    public MainWindow()
    {
        InitializeComponents();
        DataContext = this;
        SomeCommand = MyCommand() => OnAction();
    }
    

    OR you will have to update your Binding as

     <Window>
       <Window.InputBindings>
        <KeyBinding Command="{Binding SomeCommand, RelativeSource={RelativeSource Self}}" Key="F5"></KeyBinding>
       </Window.InputBindings>
     </Window>
    
    0 讨论(0)
提交回复
热议问题