Guest User

Untitled

a guest
Jan 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
  2. {
  3. var dg = sender as DataGrid;
  4. if (_hasHadInitialFocus) return;
  5.  
  6. var rowIndex = dg.Items.Count - 2;
  7. if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
  8. {
  9. var column = dg.Columns[dg.Columns.Count - 1];
  10. var item = dg.Items[rowIndex];
  11. var dataGridCellInfo = new DataGridCellInfo(item, column);
  12.  
  13. if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
  14. dg.SelectedCells.Clear();
  15. dg.SelectedCells.Add(dataGridCellInfo);
  16. }
  17. else {
  18. var row = dg.GetRow(rowIndex);
  19. row.IsSelected = true;
  20. }
  21.  
  22. dg.CurrentCell = dataGridCellInfo;
  23. dg.BeginEdit();
  24. }
  25.  
  26. _hasHadInitialFocus = true;
  27. }
  28.  
  29. private void DataGridLostFocus(object sender, RoutedEventArgs e) {
  30. _hasHadInitialFocus = false;
  31. }
  32.  
  33. private bool _hasHadInitialFocus;
  34.  
  35. private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  36. {
  37. if (!_hasHadInitialFocus)
  38. {
  39. if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
  40. {
  41. var dataGridCellInfo = new DataGridCellInfo(
  42. dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
  43.  
  44. dataGrid.SelectedCells.Clear();
  45. dataGrid.SelectedCells.Add(dataGridCellInfo);
  46. dataGrid.CurrentCell = dataGridCellInfo;
  47. dataGrid.BeginEdit();
  48. }
  49.  
  50. _hasHadInitialFocus = true;
  51. }
  52. }
  53.  
  54. private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  55. {
  56. EditCell();
  57. }
  58.  
  59. private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
  60. {
  61. if (!_hasHadInitialFocus)
  62. {
  63. e.Handled = true;
  64. EditCell();
  65. }
  66. }
  67.  
  68. private void EditCell()
  69. {
  70. if (!_hasHadInitialFocus)
  71. {
  72. if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
  73. {
  74. var dataGridCellInfo = new DataGridCellInfo(
  75. dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
  76.  
  77. dataGrid.SelectedCells.Clear();
  78. dataGrid.SelectedCells.Add(dataGridCellInfo);
  79. dataGrid.CurrentCell = dataGridCellInfo;
  80. dataGrid.BeginEdit();
  81. }
  82.  
  83. _hasHadInitialFocus = true;
  84. }
  85. }
Add Comment
Please, Sign In to add comment