How to get excel like behaviour in a DataGrid?
I am using DataGrid from codeplex in my WPF application. I have defined some DatagridTemplateColumns. The datatemplate can contain a TextBox or a ComboBox depending on the data in ViewModel.
The user can traverse the grid elements by using arrow keys. And when the user presses enter , it goes to next row. 开发者_运维百科These behaviours are fine.
The problem comes when the user comes to a particular cell by using arrow keys and types in some characters like a, b, c, etc other than Tab, Enter , etc. I want the TextBox or ComboBox to receive focus and go into edit mode automatically.
I have set IsTabStop false in DataGridCell style. So when I press Tab from one cell it goes to next cell's TextBox or ComboBox automatically. But the same doesn't happen with the arrow keys !!
I am not getting how do I do it ?
To make the cell to go into edit mode automatically you can capture preview mouse left button down on your cell and in the event handler you can set focus on the cell and select it.
You can register the event to datagrid cell as,
<Style TargetType="{x:Type datagrid:DataGridCell}" x:Key="cellStyle">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
and then in the handler you can check for keyboard modifiers as well so you can avoid scenarios such as CTRL + Click and stuff if you want.
精彩评论