c# - Wpf Edit Cell on e.Cancel - Stack Overflow

admin2025-04-16  5

I have a WPF DataGrid and I'm trying to handle a scenario where an item does not exist in my ViewModel. When this happens, I display an error message and attempt to set focus back to the DataGrid cell to allow the user to edit the cell again. The issue is that while it stays on the cell that triggered the error, it doesn't select the entire content of the cell. Here's my code:

if (!_viewModel.ItemExists(codPro))
{
    MessageBox.Show(
        "O item não existe na tabela de itens.",
        "Erro",
        MessageBoxButton.OK,
        MessageBoxImage.Error
    );
    e.Cancel = true;

    (sender as DataGrid).Dispatcher.BeginInvoke((Action)(() =>
    {
        var dataGrid = sender as DataGrid;
        if (dataGrid != null)
        {
            dataGrid.SelectedCells.Clear();
            dataGrid.SelectedCells.Add(new DataGridCellInfo(e.Row.Item, e.Column));
            dataGrid.CurrentCell = new DataGridCellInfo(e.Row.Item, e.Column);
            dataGrid.BeginEdit();

            var cellContent = dataGrid.Columns[e.Column.DisplayIndex].GetCellContent(e.Row);
            if (cellContent != null)
            {
                var cell = cellContent.Parent as DataGridCell;
                if (cell != null)
                {
                    cell.Focus();
                    var textBox = cellContent as TextBox;
                    if (textBox != null)
                    {
                        textBox.SelectAll();
                    }
                }
            }
        }
    }));

    return;
}

What am I missing or doing wrong that prevents the entire content of the cell from being selected?

I have a WPF DataGrid and I'm trying to handle a scenario where an item does not exist in my ViewModel. When this happens, I display an error message and attempt to set focus back to the DataGrid cell to allow the user to edit the cell again. The issue is that while it stays on the cell that triggered the error, it doesn't select the entire content of the cell. Here's my code:

if (!_viewModel.ItemExists(codPro))
{
    MessageBox.Show(
        "O item não existe na tabela de itens.",
        "Erro",
        MessageBoxButton.OK,
        MessageBoxImage.Error
    );
    e.Cancel = true;

    (sender as DataGrid).Dispatcher.BeginInvoke((Action)(() =>
    {
        var dataGrid = sender as DataGrid;
        if (dataGrid != null)
        {
            dataGrid.SelectedCells.Clear();
            dataGrid.SelectedCells.Add(new DataGridCellInfo(e.Row.Item, e.Column));
            dataGrid.CurrentCell = new DataGridCellInfo(e.Row.Item, e.Column);
            dataGrid.BeginEdit();

            var cellContent = dataGrid.Columns[e.Column.DisplayIndex].GetCellContent(e.Row);
            if (cellContent != null)
            {
                var cell = cellContent.Parent as DataGridCell;
                if (cell != null)
                {
                    cell.Focus();
                    var textBox = cellContent as TextBox;
                    if (textBox != null)
                    {
                        textBox.SelectAll();
                    }
                }
            }
        }
    }));

    return;
}

What am I missing or doing wrong that prevents the entire content of the cell from being selected?

Share Improve this question edited Feb 4 at 3:16 Ferry To 1,5673 gold badges39 silver badges55 bronze badges asked Feb 3 at 22:17 shiblabishiblabi 11 bronze badge 3
  • You mention "the cell that triggered the error". What action does the user take to cause this? What is the block that contains this handler? Some additional context is needed here. – IV. Commented Feb 3 at 22:37
  • @IV. I don't think the Handler is necessary because it's only a validation to check if the item the user is inputting exists in the database or not. My question is that when this error occurs, I would like the cell to be locked and not allow advancing to other columns until it is corrected (which is already happening). However, in addition to that, I would like the entire content of the cell to be selected to facilitate editing. As it is now, a tab with the wrong code generates the error message and the second tab enters edit mode with everything selected. – shiblabi Commented Feb 4 at 11:58
  • here – shiblabi Commented Feb 4 at 12:48
Add a comment  | 

1 Answer 1

Reset to default 0

I managed to fix it by adding a line of code. I think I'm very dumb.

var cell = cellContent.Parent as DataGridCell;
if (cell != null)
{
 cell.Focus();
 var textBox = cellContent as TextBox;
 if (textBox != null)
 {
  textBox.SelectAll();
  textBox.Focus(); // this line
 }
}

转载请注明原文地址:http://www.anycun.com/QandA/1744747388a87040.html