Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
- {
- var dg = sender as DataGrid;
- if (_hasHadInitialFocus) return;
- var rowIndex = dg.Items.Count - 2;
- if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
- {
- var column = dg.Columns[dg.Columns.Count - 1];
- var item = dg.Items[rowIndex];
- var dataGridCellInfo = new DataGridCellInfo(item, column);
- if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
- dg.SelectedCells.Clear();
- dg.SelectedCells.Add(dataGridCellInfo);
- }
- else {
- var row = dg.GetRow(rowIndex);
- row.IsSelected = true;
- }
- dg.CurrentCell = dataGridCellInfo;
- dg.BeginEdit();
- }
- _hasHadInitialFocus = true;
- }
- private void DataGridLostFocus(object sender, RoutedEventArgs e) {
- _hasHadInitialFocus = false;
- }
- private bool _hasHadInitialFocus;
- private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
- {
- if (!_hasHadInitialFocus)
- {
- if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
- {
- var dataGridCellInfo = new DataGridCellInfo(
- dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
- dataGrid.SelectedCells.Clear();
- dataGrid.SelectedCells.Add(dataGridCellInfo);
- dataGrid.CurrentCell = dataGridCellInfo;
- dataGrid.BeginEdit();
- }
- _hasHadInitialFocus = true;
- }
- }
- private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
- {
- EditCell();
- }
- private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
- {
- if (!_hasHadInitialFocus)
- {
- e.Handled = true;
- EditCell();
- }
- }
- private void EditCell()
- {
- if (!_hasHadInitialFocus)
- {
- if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
- {
- var dataGridCellInfo = new DataGridCellInfo(
- dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
- dataGrid.SelectedCells.Clear();
- dataGrid.SelectedCells.Add(dataGridCellInfo);
- dataGrid.CurrentCell = dataGridCellInfo;
- dataGrid.BeginEdit();
- }
- _hasHadInitialFocus = true;
- }
- }
Add Comment
Please, Sign In to add comment