none
WPF按键绑定 RRS feed

  • 问题

  • <Window.InputBindings> 
            <KeyBinding Key="Up" Command="vm:GameViewModel.BlocksTransformationCommand" x:Name="_111"/>
            <KeyBinding Key="Left" Command="vm:GameViewModel.BlocksLeftCommand" />
            <KeyBinding Key="Right" Command="vm:GameViewModel.BlocksRightCommand" />
            <KeyBinding Key="Down" Command="vm:GameViewModel.BlocksDownCommand" />
            <KeyBinding Key="F1" Command="vm:GameViewModel.BlocksStopCommand" />
    </Window.InputBindings>

    我知道按键绑定可以按照上面的这么写进行数据绑定,然后在<Window.CommandBindings>xxx</Window.CommandBindings>中写相关方法的绑定。

    但是如果我需要让用户自己设置按键,我需要怎么写?

    我是这么想的,现在类中实现相应按键的属性,然后在xaml进行数据绑定(如下),但是显示不能绑定,所以我需要怎么做?

     <Window.InputBindings>
            <KeyBinding Key="{Binding TransformationKey}" Command="vm:GameViewModel.BlocksTransformationCommand" x:Name="_111"/>
            <KeyBinding Key="{Binding LeftKey}" Command="vm:GameViewModel.BlocksLeftCommand" />
            <KeyBinding Key="{Binding RightKey}" Command="vm:GameViewModel.BlocksRightCommand" />
            <KeyBinding Key="{Binding DownKey}" Command="vm:GameViewModel.BlocksDownCommand" />
            <KeyBinding Key="{Binding StopKey}" Command="vm:GameViewModel.BlocksStopCommand" />
        </Window.InputBindings>



    • 已编辑 Aragaki 2017年7月10日 5:39 前面写错了
    2017年7月10日 5:38

答案

  • Hi,

    >> 我是这么想的,现在类中实现相应按键的属性,然后在xaml进行数据绑定(如下),但是显示不能绑定,所以我需要怎么做?

    我认为你的想法是可以的,但是注意,设置好的数据应该持久化保持起来。我做了简单的绑定,我这边是可以绑定的,下面是我的代码:

    namespace WpfTest
    {
        /// <summary>
        /// Interaction logic for Window25.xaml
        /// </summary>
        public partial class Window25 : Window
        {
            public Window25()
            {
                InitializeComponent();
    
                Window25ViewModel vm = new Window25ViewModel();
                vm.keys.TransformationKey = Key.A;
                vm.keys.LeftKey = Key.B;
                vm.keys.RightKey = Key.C;
                vm.keys.DownKey = Key.D;
                vm.keys.StopKey = Key.E;
                this.DataContext = vm;
            }
        }
        public class Window25ViewModel
        {
            public KeySetting keys
            {
                get;
                set;
            }
            public Window25ViewModel()
            {
                keys = new KeySetting();
            }
    
            private ICommand _blocksTransformationCommand;
            public ICommand BlocksTransformationCommand
            {
                get
                {
                    if (_blocksTransformationCommand == null)
                    {
                        _blocksTransformationCommand = new RelayCommand(
                           SetblocksTransformation,
                            obj => true);
                    }
                    return _blocksTransformationCommand;
                }
            }
            public void SetblocksTransformation(object obj)
            {
                MessageBox.Show("set Transformation successfully");
            }
    
    
            private ICommand _blocksLeftCommand;
            public ICommand BlocksLeftCommand
            {
                get
                {
                    if (_blocksLeftCommand == null)
                    {
                        _blocksLeftCommand = new RelayCommand(
                           SetBlocksLeftCommand,
                            obj => true);
                    }
                    return _blocksLeftCommand;
                }
            }
            public void SetBlocksLeftCommand(object obj)
            {
                MessageBox.Show("set successfully");
            }
    
            private ICommand _blocksRightCommand;
            public ICommand BlocksRightCommand
            {
                get
                {
                    if (_blocksRightCommand == null)
                    {
                        _blocksRightCommand = new RelayCommand(
                           SetBlocksRightCommand,
                            obj => true);
                    }
                    return _blocksRightCommand;
                }
            }
            public void SetBlocksRightCommand(object obj)
            {
                MessageBox.Show("set Right successfully");
            }
    
    
    
            private ICommand _blocksDownCommand;
            public ICommand BlocksDownCommand
            {
                get
                {
                    if (_blocksDownCommand == null)
                    {
                        _blocksDownCommand = new RelayCommand(
                           SetBlocksDownCommand,
                            obj => true);
                    }
                    return _blocksDownCommand;
                }
            }
            public void SetBlocksDownCommand(object obj)
            {
                MessageBox.Show("set Down successfully");
            }
    
    
            private ICommand _blocksStopCommand;
            public ICommand BlocksStopCommand
            {
                get
                {
                    if (_blocksStopCommand == null)
                    {
                        _blocksStopCommand = new RelayCommand(
                           SetBlocksStopCommand,
                            obj => true);
                    }
                    return _blocksStopCommand;
                }
            }
            public void SetBlocksStopCommand(object obj)
            {
                MessageBox.Show("set stop successfully");
            }
        }
    
        public class RelayCommand : ICommand
        {
            private Predicate<object> _canExecute;
            private Action<object> _execute;
    
            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                this._canExecute = canExecute;
                this._execute = execute;
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            public bool CanExecute(object parameter)
            {
                return _canExecute(parameter);
            }
    
            public void Execute(object parameter)
            {
                _execute(parameter);
            }
        }
    
        public class KeySetting : INotifyPropertyChanged
        {
    
            private Key _transformationKey;
            public Key TransformationKey
            {
                get { return _transformationKey; }
                set
                {
                    _transformationKey = value;
                    raisePropertyChanged("TransformationKey");
                }
            }
            private Key _leftKey;
            public Key LeftKey
            {
                get { return _leftKey; }
                set
                {
                    _leftKey = value;
                    raisePropertyChanged("LeftKey");
                }
            }
            private Key _rightKey;
            public Key RightKey
            {
                get { return _rightKey; }
                set
                {
                    _rightKey = value;
                    raisePropertyChanged("RightKey");
                }
            }
            private Key _downKey;
            public Key DownKey
            {
                get { return _downKey; }
                set
                {
                    _downKey = value;
                    raisePropertyChanged("DownKey");
                }
            }
            private Key _stopKey;
            public Key StopKey
            {
                get { return _stopKey; }
                set
                {
                    _stopKey = value;
                    raisePropertyChanged("StopKey");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            public void raisePropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
      <Window.InputBindings>
            <KeyBinding Key="{Binding keys.TransformationKey}" Command="{Binding BlocksTransformationCommand}"/>
            <KeyBinding Key="{Binding keys.LeftKey}" Command="{Binding BlocksLeftCommand}" />
            <KeyBinding Key="{Binding keys.RightKey}" Command="{Binding BlocksRightCommand}"/>
            <KeyBinding Key="{Binding keys.DownKey}" Command="{Binding BlocksDownCommand}" />
            <KeyBinding Key="{Binding keys.StopKey}" Command="{Binding BlocksStopCommand}"/>
        </Window.InputBindings>

    在程序启动是你应该从文件中读出设置好的键值来填充。

    用户设置热键是,可以在keydown事件中做,也可以使用钩子全局捕获用户按下的键值。

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Aragaki 2017年7月17日 1:37
    2017年7月10日 9:18
    版主