【WPF/WAF】设置快捷键

70 篇文章 5 订阅

基于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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值