C#wpf dataGrid 单元格内容的快捷键复制粘贴 使用的是Prism框架
xaml 代码
<Window x:Class="DataGridOption.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525" KeyDown="Window_KeyDown">
<Grid>
<DataGrid ItemsSource="{Binding DataGridList}"
SelectionUnit="Cell" >
<DataGrid.Columns>
<DataGridTextColumn Header="School" Width="Auto" IsReadOnly="True" Binding="{Binding School,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<DataGridTextColumn Header="Name" Width="Auto" Binding="{Binding Name,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<DataGridTextColumn Header="Avg" Width="Auto" Binding="{Binding Avg,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<DataGridTextColumn Header="Class" Width="Auto" Binding="{Binding Class,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
dataGrid数据绑定使用的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataGridOption
{
public class DataGridData
{
public string School { get; set; }
public string Name { get; set; }
public string Avg { get; set; }
public string Class { get; set; }
}
}
ViewModel代码为
using Prism.Mvvm;
using System.Collections.ObjectModel;
namespace DataGridOption.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
private ObservableCollection<DataGridData> _DataGridList;
public ObservableCollection<DataGridData> DataGridList
{
get => _DataGridList;
set => SetProperty(ref _DataGridList, value);
}
public MainWindowViewModel()
{
ObservableCollection<DataGridData> dataGrids = new ObservableCollection<DataGridData>();
dataGrids.Add(new DataGridData()
{ School = "湖南大学", Name = "john", Avg = "20", Class = "大二" });
dataGrids.Add(new DataGridData()
{ School = "中南大学", Name = "jim", Avg = "22", Class = "大三" });
dataGrids.Add(new DataGridData()
{ School = "湖南师范大学", Name = "godv", Avg = "23", Class = "大四" });
dataGrids.Add(new DataGridData()
{ School = "国防科技大学", Name = "yhan", Avg = "19", Class = "大一" });
_DataGridList = dataGrids;
}
}
}
窗体的代码
using System.Windows;
using System.Windows.Input;
namespace DataGridOption.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
ModifierKeys keyboard = Keyboard.Modifiers;
Key key = e.Key;
if (keyboard == ModifierKeys.Control && key == Key.C)
{
// 在这里编写您想要执行的复制操作
MessageBox.Show("复制");
e.Handled = true; // 防止触发默认的复制操作
}
if (keyboard == ModifierKeys.Control && key == Key.V)
{
// 在这里编写您想要执行的粘贴操作
MessageBox.Show("粘贴");
e.Handled = true; // 防止触发默认的粘贴操作
}
}
}
}
因为项目需要使用Ctrl+C 以及Ctrl+V 实现复制粘贴 但是按照网上方法,keydown一直监听不到C的按下操作,我如果改成Ctrl+s则就能显示复制,想请教下,这个是咋回事呢