首页 > Windows开发 > 详细

【WPF/WAF】设置快捷键(Shortcut Key)

时间:2020-04-21 09:02:23      阅读:86      评论:0      收藏:0      [点我收藏+]
原文:【WPF/WAF】设置快捷键(Shortcut Key)

基于WAF框架:WPF Application Framework (WAF)

View层XAML中设置热键。

    <Window.InputBindings>
        <!--<KeyBinding Command="{Binding SaveCommand}" Key="S" Modifiers="Control"/>-->
        <KeyBinding Command="{Binding AboutCommand}" Key="F1"/>
    </Window.InputBindings>

ViewModel中定义该AboutCommand命令。


        private ICommand aboutCommand;
        public ICommand AboutCommand
        {
            get { return aboutCommand; }
            set { SetProperty(ref aboutCommand, value); }
        }

控制层写AboutCommand命令的实现。

namespace WafApplication1.Applications.Controllers
{
    [Export]
    internal class ApplicationController
    {
        private readonly ShellViewModel shellViewModel;
        private readonly DelegateCommand aboutCommand;

        [ImportingConstructor]
        public ApplicationController(ShellViewModel shellViewModel)
        {
            this.shellViewModel = shellViewModel;
            this.aboutCommand = new DelegateCommand(AboutCommand);
        }

        private void AboutCommand()
        {
            MessageBox.Show("F1 Command!");
        }

        public void Initialize()
        {
            shellViewModel.AboutCommand = this.aboutCommand;
        }

        public void Run()
        {
            shellViewModel.Show();
        }

        public void Shutdown()
        {
        }
    }
}

运行该项目,按F1即可看到弹出弹窗。

技术分享图片


新的问题

给该Window窗体注册的快捷键,必须要在该窗体获得焦点时快捷键才有效。如果该窗体内有别的控件(如ListBox)获取了焦点,再点击该快捷键将不起效果。这时候,可考虑同样给该ListBox控件添加相同的快捷键命令。

<!-- 快捷键 -->
<ListBox.InputBindings>
    <KeyBinding Command="{Binding ShortcutScaleCommand}" Key="F1"/>
</ListBox.InputBindings>

【WPF/WAF】设置快捷键(Shortcut Key)

原文:https://www.cnblogs.com/lonelyxmas/p/12741810.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!