如何将“按 Escape 键时关闭"行为分配给项目中的所有 WPF 窗口? [英] How can I assign the 'Close on Escape-key press' behavior to all WPF windows within a project?

查看:23
本文介绍了如何将“按 Escape 键时关闭"行为分配给项目中的所有 WPF 窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Is there any straightforward way of telling the whole WPF application to react to Escape key presses by attempting to close the currently focused widow? It is not a great bother to manually setup the command- and input bindings but I wonder if repeating this XAML in all windows is the most elegant approach?

<Window.CommandBindings>
        <CommandBinding Command="Close" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
        <KeyBinding Key="Escape" Command="Close" />
</Window.InputBindings>

Any constructive suggestions welcome!

解决方案

All I can suggest to improve on that is to remove the need for an event handler by binding to a static command instance.

Note: this will only work in .NET 4 onwards as it requires the ability to bind to the KeyBinding properties.

First, create a command that takes a Window as a parameter and calls Close within the Execute method:

public class CloseThisWindowCommand : ICommand
{
    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        //we can only close Windows
        return (parameter is Window);
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (this.CanExecute(parameter))
        {
            ((Window)parameter).Close();
        }
    }

    #endregion

    private CloseThisWindowCommand()
    {

    }

    public static readonly ICommand Instance = new CloseThisWindowCommand();
}

Then you can bind your KeyBinding to the static Instance property:

<Window.InputBindings>
    <KeyBinding Key="Escape" Command="{x:Static local:CloseThisWindowCommand.Instance}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
</Window.InputBindings>

I don't know that this is necessarily better than your approach, but it does mean marginally less boilerplate at the top of every Window and that you don't need to include an event handler in each

这篇关于如何将“按 Escape 键时关闭"行为分配给项目中的所有 WPF 窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆