WPF 组合快捷键(Ctrl+C)

时间:2022-07-21 22:29:36

页面程序:

<Window x:Class="WpfDataGrid.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <RoutedUICommand x:Key="btnClick" Text="Button Click"/> 
    </Window.Resources>
    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+C" Key="C" Command="{StaticResource btnClick}"/>
    </Window.InputBindings>
    <Window.CommandBindings>
        <CommandBinding Command="{StaticResource btnClick}"
                        CanExecute="CommandBinding_CanExecute"
                        Executed="CommandBinding_Executed"/>
    </Window.CommandBindings>
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="105,76,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfDataGrid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            main.ShowDialog();
        }

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            button1_Click(this, null);
        }
    }
}