WPF中设置快捷键
方式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中设置快捷键的更多相关文章
- Web开发中设置快捷键来增强用户体验
从事对日外包一年多以来,发现日本的无论是WinForm项目还是Web项目都注重快捷键的使用,日本人操作的时候都喜欢用键盘而不是用鼠标去点,用他们的话来说"键盘永远比鼠标来的快",所 ...
- wpf中实现快捷键
<Window.InputBindings> <KeyBinding Gesture="Ctrl+Alt+Q" Command="{Binding Yo ...
- WPF中设置了WindowStyle="None"后,窗口仍然有边框的解决方法
1. 设置了窗体的WindowStyle="None",窗口还是右边框,如下图: 2. 这是因为窗体默认是可以改变大小的,所以需要修改ResizeMode的值 ResizeMode ...
- android中设置快捷键方法setShortcut参数的说明
setShortcut之所以要两个参数来设定两个快捷键是为了应对不同的手机键盘.数字快捷键为12键键盘(0~9,*,#,共12个按键)所准备的.其实我怀疑有这种键盘的手机能装Android么?因为我的 ...
- WPF中设置Border的BorderThickness属性会让背景图片产生模糊感
<!--设置BorderThickness会让border的Background图片看起来有模糊感--> <Border x:Name="border" Bord ...
- WPF中的快捷键(累积中)
1. 显示可选属性, ctrl + J 如上图,当不知道Background的可选择时,可以输入 ctrl + J,系统就会显示所有可选属性
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 WPF中不规则窗体与WebBrowser控件的兼 ...
- 【WPF/WAF】设置快捷键(Shortcut Key)
基于WAF框架:WPF Application Framework (WAF) View层XAML中设置热键. <Window.InputBindings> <!--<KeyB ...
- WPF中通过代码设置控件的坐标
用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...
随机推荐
- [Spring MVC] - Spring MVC环境搭建
1) 复制Spring所需要的lib包 (这是SSH所需要的lib包,如果只使用spring,可以移除一些包) 2) 配置web.xml <?xml version=" ...
- JavaScript常用小技巧
1.获取访问地址URL的参数 <script type="text/javascript"> var param = ""; var nowUrl ...
- ADF_ManagedBean的概念和管理(概念)
20150623 Created By BaoXinjian
- [ActionScript 3.0] AS3.0 简单封装Socket的通信
Socket服务器 package com.controls.socket { import com.models.events.AppEvent; import com.models.events. ...
- linux iostat 性能指标说明
Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...
- (DP)3.Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- .net一次连接执行多条sql语句
方法一: string SQLString="select 1; select 2;"; using (OdbcConnection connection = new OdbcCo ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- Android.mk的用法和基础【转】
一个Android.mk file用来向编译系统描述你的源代码.具体来说:该文件是GNU Makefile的一小部分,会被编译系统解析一次或多次.你可以在每一个Android.mk file中定义一个 ...
- Qt 环境下的activex控件编程-------1
本人第一次接触这种activeX控件的东西,参考了网上很多的教程,终于耗时三个多小时初步理解并编写了一个小demo,现在分享给大家,希望大家少走弯路.步骤如下: 1>像平常创建项目一样创建一个d ...