WPF中设置快捷键

简介: 原文:WPF中设置快捷键方式1: 窗体中加入资源                                                                                                        其中:CommandBindin...
原文: 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中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
WPF界面异常:未将对象引用设置到对象实例
WPF界面异常:未将对象引用设置到对象实例
WPF TreeView设置所有节点默认展开
WPF TreeView设置所有节点默认展开
|
C#
WPF Image Source 设置相对路径图片
原文:WPF Image Source 设置相对路径图片   BitmapImage bt = new BitmapImage(new Uri("Images\\3_u10484.png", UriKind.Relative));this.Img1.Source = bt;
4024 0
|
C#
WPF透明设置(Opacity)
原文:WPF透明设置(Opacity) 渐变透明 ...
1480 0
|
C# 前端开发
WPF 和 UWP 中,不用设置 From 或 To,Storyboard 即拥有更灵活的动画控制
原文:WPF 和 UWP 中,不用设置 From 或 To,Storyboard 即拥有更灵活的动画控制 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
968 0
|
C#
wpf datagrid设置右键菜单打开时选中项的背景色
原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangli321456/article/details/53929433 ...
1468 0
|
C#
WPF DataGrid 每行ComboBox 内容不同的设置方法
原文:WPF DataGrid 每行ComboBox 内容不同的设置方法 ...
1101 0
|
C#
继续聊WPF——设置网格控件列标题的样式
原文:继续聊WPF——设置网格控件列标题的样式   我很奇怪的是,微软那厮是怎么搞的,Blend里面居然不能编辑GridView的样式,十万般无奈之下,只好手写XAML来处理了。
1029 0