To implement the same way of moving the WPF DataGrid as Excel, You need to override the OnPreviewKeyDown event of the DataGrid:
First, create a class ExDataGrid.
The Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows. Controls;
Using System. Windows. Input;
Using System. Windows;
Namespace WpfDataGrid
{
Public class ExDataGrid: DataGrid // inherits the control
{
Protected override void OnPreviewKeyDown (KeyEventArgs e)
{
If (e. key = Key. left | e. key = Key. right | e. key = Key. up | e. key = Key. down)
{
Try
{
Base. commitedit ();
}
Catch (exception ex)
{
Base. canceledit ();
String mess = ex. Message;
If (ex. InnerException! = Null)
Mess + = "/n" + ex. InnerException. Message;
MessageBox. Show (mess );
}
} Base. OnPreviewKeyDown (e );
}
}
}
Add the reference "xmlns: UC =" clr-namespace: WpfDataGrid "" on the page where a new DataGrid is required "]
The specific implementation page is:
<Window x: Class = "WpfDataGrid. Window2"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: UC = "clr-namespace: WpfDataGrid"
Title = "Window2" Height = "300" Width = "300">
<Grid>
<! -- <Button content = "button (F1)" Height = "23" horizontalalignment = "Left" name = "button1" verticalalignment = "TOP" width = "75"/> -->
<UC: exdatagrid X: Name = "DataGrid" itemssource = "{binding}" autogeneratecolumns = "false" selectionunit = "cell" selectionmode = "extended">
<UC: exdatagrid. Columns>
<Maid header = "name" width = "80" binding = "{binding name}"/>
<Maid header = "Age" width = "50" binding = "{binding age}"/>
<DataGridCheckBoxColumn Header = "Pass Exam? "Width =" 100"
Binding = "{Binding Pass}"/>
<DataGridHyperlinkColumn Header = "Email" Width = "150"
Binding = "{Binding Email}"/>
</UC: ExDataGrid. Columns>
</UC: ExDataGrid>
</Grid>
</Window>
To facilitate the entry of false data in the test background, the Code is as follows:
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;
Using System. Collections. ObjectModel;
Namespace WpfDataGrid
{
/// <Summary>
/// Interaction logic for Window2.xaml
/// </Summary>
Public partial class Window2: Window
{
Public Window2 ()
{
InitializeComponent ();
// This. Loaded + = new RoutedEventHandler (Window2_Loaded );
ObservableCollection <Member> memberData = new ObservableCollection <Member> ();
MemberData. Add (new Member ()
{
Name = "Joe ",
Age = "23 ",
// Sex = SexOpt. Male,
Pass = true,
Email = new Uri ("mailto: Joe@school.com ")
});
MemberData. Add (new Member ()
{
Name = "Mike ",
Age = "20 ",
// Sex = SexOpt. Male,
Pass = false,
Email = new Uri ("mailto: Mike@school.com ")
});
MemberData. Add (new Member ()
{
Name = "Lucy ",
Age = "25 ",
// Sex = SexOpt. Female,
Pass = true,
Email = new Uri ("mailto: Lucy@school.com ")
});
DataGrid. DataContext = memberData;
}
}
Public enum SexOpt {Male, Female };
Public class Member
{
Public string Name {get; set ;}
Public string Age {get; set ;}
Public SexOpt Sex {get; set ;}
Public bool Pass {get; set ;}
Public Uri Email {get; set ;}
}
}
This is fine, isn't it easy.