【问题标题】:Create Key binding in WPF在 WPF 中创建键绑定
【发布时间】:2013-11-10 21:40:48
【问题描述】:

我需要为 Window 创建输入绑定。

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

    public ICommand SomeCommand { get; private set; }

    public void OnAction()
    {
        SomeControl.DoSomething();
    }
}

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

如果我用一些CustomCommand : ICommand 初始化SomeCommand,它不会触发。 SomeCommand 属性 getter 永远不会被调用。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    对于您的情况,最好使用 MVVM 模式

    XAML:

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

    后面的代码:

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

    在您的视图模型中:

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

    然后你需要一个ICommand 的实现。 这个简单实用的课程。

    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;
    }   
    

    【讨论】:

    • 是否有任何快速简单的方法在任何特定的组合键上运行 void buttonPress() { MessageBox.Show("It works"); 方法,例如Ctrl+A?过去 20 分钟我一直在谷歌搜索,但我找不到一个简单的例子来做到这一点。
    • @MKII 不要忘记将DataContext = this; 添加到Mainwindow 方法中
    【解决方案2】:

    对于修饰符(组合键):

    <KeyBinding Command="{Binding SaveCommand}" Modifiers="Control" Key="S"/>
    

    【讨论】:

      【解决方案3】:

      可能为时已晚,但这是最简单、最短的解决方案。

      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" >
      

      【讨论】:

      • 它没有用。 e.KeyLeftCtrlRightCtrl 因为只要按下修改键就会触发它
      • 这不是在触发您感兴趣的实际事件后检查按下的键盘修改键的当前状态吗?按键事件的状态应该在KeyEventArgs 中,包括使用的修饰符。 Keyboard 这里看起来像一个(静态/本地)属性或静态类,我没有检查 API 的样子,但这可能是 @Zaheer 说这不起作用的原因。
      • hmm.... 切换if 中的参数怎么样?所以修饰符只有在Key == Key.S
      【解决方案4】:

      您必须创建自己的Command 实现ICommand 接口并使用Command 的实例初始化SomeCommand

      现在您必须将 Window 的 DataContext 设置为 self 才能使 Command Binding 工作:

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

      或者您必须将您的Binding 更新为

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

      【讨论】:

        【解决方案5】:

        这就是我在项目中解决这个问题的方法:

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

        ReportCommandMyView 中的 ICommand不是 在 ViewModel 中。

        【讨论】:

          猜你喜欢
          • 2014-05-16
          • 2011-07-03
          • 1970-01-01
          • 2011-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-01
          相关资源
          最近更新 更多