方式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. IOS开发-本地通知

    // 注册 发送通知的方法 -(void)pushNotfation{ //--------------初始化本地通知 alloc init 虽然是UI控件 但继承NSObject UILocalNo ...

  2. Property ClientHeight does not exist 问题解决

    delphi的TFrame继承自另一个TFrame时,最好通过File->New->Other...->Delphi Projects->Inheritable Items 的 ...

  3. Joint Deep Learning for Pedestrian Detection笔记

    1.结构图 Introduction Feature extraction, deformation handling, occlusion handling, and classification ...

  4. C++11:POD数据类型

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 啥是POD类型? POD全称Plain Old Data.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是 ...

  5. HoloLens开发笔记之Gesture input手势输入

    手势是HoloLens三个首要输入形式之一.一旦你使用凝视定位了一个全息图像,手势允许你与它交互.手势输入允许你使用手或者点击器原生地与全息图像交互. 手势之外,你也可以在应用中使用语音输入来交互. ...

  6. gerrit 修改前一次提交的方法(转载)

    From:http://sinojelly.sinaapp.com/2011/08/git-changes-submitted-by-the-previous-method-pay-special-a ...

  7. Eclipse启动Tomcat后无法访问项目

    Eclipse中的Tomcat可以正常启动,不过发布项目之后,无法访问,包括http://localhost:8080/的小猫页面也无法访问到,报404错误.这是因为Eclipse所指定的Server ...

  8. windows下python3.4安装scikit-learn

    python3.4.0_64位下安装numpy-1.11.1 安装步骤: ​1.在终端CMD中输入: python -m pip install -U pip​​ 2.找到 下载的 numpy-1.1 ...

  9. [ActionScript 3.0] AS3.0 简单封装Socket的通信

    Socket服务器 package com.controls.socket { import com.models.events.AppEvent; import com.models.events. ...

  10. silverlight简单数据绑定1

    数据绑定是用户界面与数据源之间的媒介:通过绑定可以使数据在界面和数据源之间传递交流.数据绑定由System.Windows.Data命名空间的Binding对象完成. 创建绑定的数据对象类. .cs类 ...