方式1:

窗体中加入资源

<UserControl.Resources>
        <RoutedUICommand x:Key="Cut" Text="剪切" />
        <RoutedUICommand x:Key="Copy" Text="复制" />
        <RoutedUICommand x:Key="Paste" Text="粘贴" />
        <RoutedUICommand x:Key="Select" Text="全选" />
    </UserControl.Resources>
    <UserControl.InputBindings>
        <KeyBinding Gesture="Ctrl+X" Command="{StaticResource Cut}" />
        <KeyBinding Gesture="Ctrl+C" Command="{StaticResource Copy}" />
        <KeyBinding Gesture="Ctrl+V" Command="{StaticResource Paste}" />
    </UserControl.InputBindings>
    <UserControl.CommandBindings>
        <CommandBinding Command="{StaticResource Cut}" Executed="CommandBinding_Cut"></CommandBinding>
        <CommandBinding Command="{StaticResource Copy}" Executed="CommandBinding_Copy"></CommandBinding>
        <CommandBinding Command="{StaticResource Paste}" Executed="CommandBinding_Paste"></CommandBinding>
    </UserControl.CommandBindings>

其中:CommandBinding_Cut ,CommandBinding_Copy ,CommandBinding_Paste 是按下快捷键对用的事件操作

private void CommandBinding_Cut(object sender, ExecutedRoutedEventArgs e)
 {
           
 }

方式2:

写控件或者窗体的KeyDown事件 PreviewKeyDown="Window_KeyDown"

private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {

if (e.Key == Key.Enter)
                {
                    //搜索
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement == SearchTxt) Search_Click(SearchBtn, e);
                    //文件或者文件夹重命名
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox")
                    {
                        TextBox box = Keyboard.FocusedElement as TextBox;
                        FilesModel model = box.DataContext as FilesModel;
                        if (model != null) ReName_LostFocus(box, e);
                    }

Keyboard.ClearFocus();
                }
                //Ctrl+C 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.C))
                {
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox") return;
                    CommandBinding_Copy(null, null);
                }

//Ctrl+X 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.X))
                {
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox") return;
                    CommandBinding_Cut(null, null);
                }
                //Ctrl+V 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.V))
                {
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox") return;
                    CommandBinding_Paste(null, null);
                }

//Ctrl+A 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.A))
                {
                    SelectAllCheck.IsChecked = true;
                    SelectAll_Click(SelectAllCheck, e);
                }
                //Shift+D 删除
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)) && e.KeyboardDevice.IsKeyDown(Key.Delete))
                {
                    DeleteBtn_Click(null, e);
                }
            }
            catch (Exception)
            {
            }

}

WPF中设置快捷键的更多相关文章

  1. Web开发中设置快捷键来增强用户体验

    从事对日外包一年多以来,发现日本的无论是WinForm项目还是Web项目都注重快捷键的使用,日本人操作的时候都喜欢用键盘而不是用鼠标去点,用他们的话来说"键盘永远比鼠标来的快",所 ...

  2. wpf中实现快捷键

    <Window.InputBindings> <KeyBinding Gesture="Ctrl+Alt+Q" Command="{Binding Yo ...

  3. WPF中设置了WindowStyle="None"后,窗口仍然有边框的解决方法

    1. 设置了窗体的WindowStyle="None",窗口还是右边框,如下图: 2. 这是因为窗体默认是可以改变大小的,所以需要修改ResizeMode的值 ResizeMode ...

  4. android中设置快捷键方法setShortcut参数的说明

    setShortcut之所以要两个参数来设定两个快捷键是为了应对不同的手机键盘.数字快捷键为12键键盘(0~9,*,#,共12个按键)所准备的.其实我怀疑有这种键盘的手机能装Android么?因为我的 ...

  5. WPF中设置Border的BorderThickness属性会让背景图片产生模糊感

    <!--设置BorderThickness会让border的Background图片看起来有模糊感--> <Border x:Name="border" Bord ...

  6. WPF中的快捷键(累积中)

    1. 显示可选属性, ctrl + J 如上图,当不知道Background的可选择时,可以输入 ctrl + J,系统就会显示所有可选属性

  7. WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案

    首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如  WPF中不规则窗体与WebBrowser控件的兼 ...

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

    基于WAF框架:WPF Application Framework (WAF) View层XAML中设置热键. <Window.InputBindings> <!--<KeyB ...

  9. WPF中通过代码设置控件的坐标

    用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...

随机推荐

  1. WebApi:使用方法名或者控制器名作为接口地址

    今天遇到一个问题:新建的WebApi的项目生成的接口的地址都是以控制器的名字命名的,这样的话,在方法前添加ActionName就不起作用了,但之前一个项目是可以的. 接口代码: public clas ...

  2. .Net 一些好工具

    1.代码类 ReSharper :代码检查,代码补齐,(收费)(vs插件) MultiEditing: vs多行编译工具 VSCommand : Web Essentials : Css扩展支持,JS ...

  3. ubuntu16.04解决播放swf视频文件问题

    使用下面 sudo apt-get install swfdec-gnome

  4. 分享一下自己正在使用的sublime text里的插件

    真的回头想想要不是当时一个学姐给我介绍了这个编辑器,我可能还是那种迫不得已了不得不编程了才会去敲代码的,可能还是一只不喜欢编程的程序员.可是自从用了这款编辑器,我的世界仿佛都被改变了.天呐,整天忍不住 ...

  5. 转:Android Webview 加载外部html时选择加载本地的js,css等资源文件

    原文地址:http://m.blog.csdn.net/blog/qduningning/43196819 在使用WebView加载网页的时候,有一些固定的资源文件如js的jquery包,css,图片 ...

  6. iScroll滚动区域中select、input、textarea元素无法点击的Bug修复

    最近在一个项目中使用了iScroll4模拟滚动效果,调试过程中发现一个表单页中的所有表单项都无法点击聚焦, 如<select>.<input>.<textarea> ...

  7. mac 下 终端常用命令

    open . 打开当前路径下的文件夹 open (路径) 打开此路径下的文件夹 (路径:/Users/linger/Library/Application\ Support/Google/Chrome ...

  8. TCP报文段首部详解

    TCP虽然是面向字节流的,但是tcp传送的数据单元却是报文段,一个报文段分为首部和数据两部分,几乎TCP所有功能都从首部来体现,下面我们来详细的剖析下它的首部. (1):源端口与目标端口:分别写入源端 ...

  9. Python在安装第三方模块遇到的问题及解决办法

    今天在安装第一个模块psutil的时候出现了以下问题: 1.找不到Python.h文件 解决办法:重新安装python环境:sudo apt-get install python-dev 说明:网上有 ...

  10. 自定义控件EditText

    public class defineEditText extends EditText { Context context; Drawable d; public defineEditText(Co ...