WPFでフォーカスを移動するには、TABキーまたはマウスクリックが標準。
DataGridも同じ。これが結構面倒。
特に自分のPCにはテンキーが付いているが、TABキーを使うとなると、効果半減。
バタバタ入力するには、ヤッパリ Enterキーでフォーカス移動したい。
同じような考えの人はいるもので、ここにヒントが。ただリンク先の方法そのままでは、フォーカスが右下方向に移動してしまう。(SilverLightとWPFの違いかな?)
自分がやりたいのは、DataGrid全体が入力項目で
- Enterキーで、フォーカスを右へ
- 右端の項目で、Enterキー押下の場合 次行の先頭へ
前述のサイトを参考に、作ったコントロールを下に示す。
using System.Windows.Controls; |
using System.Windows.Input; |
public class CustomDataGrid : DataGrid |
public CustomDataGrid() : base () |
protected override void OnKeyDown(KeyEventArgs e) |
private void MoveNextCell() |
DataGridColumn currentcol = this .CurrentColumn; |
bool isLastCol = (currentcol.DisplayIndex == this .Columns.Count - 1); |
this .CurrentColumn = this .Columns[currentcol.DisplayIndex + 1]; |
int currentrow = this .Items.IndexOf( this .SelectedItem); |
int rowMax = this .Items.Count; |
if ((currentrow + 1) != rowMax) |
this .SelectedIndex = currentrow + 1; |
this .CurrentCell = new DataGridCellInfo( this .Items[currentrow + 1], this .Columns[0]); |
参考までに XAMLでの指定は
xmlns:my="clr-namespace:mbpm.classes" |
< my:CustomDataGrid ・・・・・> |
参考URL
https://social.msdn.microsoft.com/Forums/ja-JP/e9909a42-ce31-4b1f-9443-d5fa77c76a77/datagridenter?forum=silvelightdotnetja
http://gacken.com/wp/program/wpf/570/