简体   繁体   中英

How to restore focus on cell in WPF DataGrid after refresh of the items

I'm developing a WPF application with VS2015.

For validating columns with minimal and maximal values in a WPF datagrid i'm using a ValidationRule.

Here the code for the ValidationRule:

public class MinMaxValidationRule : System.Windows.Controls.ValidationRule
{
    /// <summary>
    /// Validates updated values and compares min and max values.
    /// </summary>
    /// <param name="value"></param>
    /// <param name="cultureInfo"></param>
    /// <returns></returns>
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        BindingExpression bindingExpr = value as BindingExpression;
        if (bindingExpr != null)
        {
            var item = bindingExpr.DataItem as XraySystemStructure;
            if (item != null &&
                item.countMandatory > item.countMax)
            {
                // The min value is bigger than the max value -> disables the save-button and displays error
                SaveButtonEnabled = false;
                return new ValidationResult(false, TextCountMandatoryBiggerThanCountMax);
            }

            // Validation is correct -> Refreshes the datagrid to remove all errors (multiple datagridcells)
            SaveButtonEnabled = true;
            grdSystemStructure.Items.Refresh();
        }

        // Validation is correct -> Remove all errors
        return new ValidationResult(true, null);
    }
}

In the validation i'm checking if the value in countMandatory is bigger than the value in countMax.

If countMandatory is bigger this is an error.

At runtime it is possible that both cells are marked with validationerror because the user can enter in countMax a value which is smaller than countMandatory which is wrong.

So - for removing in case of validation success - all validation errors i have to use a refresh on all items of the datagrid.

By that i'm losing the focus on the datagrid so the user must click on the cell to continue his input in the current cell.

Per example:

A user entered countMandatory "2" and maxCount "1".

The maxCount column is marked with validation error.

Then the user wants to edit maxCount to "111", clicks in the maxCount-cell and adds another "1" so that maxCount is now "11" and the validation is successful.

But by refreshing the items the datagrid has lost the focus and the user must click with the mouse in the cell to continue the last "1" which is not user-friendly.

How can i restore the focus on the current cell? I have tried to set SelectedItem and CurrentCell after the refresh but it don't worked.

Per example that solution which doesn't fix my problem:

            grdSystemStructure.Items.Refresh();
            grdSystemStructure.ScrollIntoView(item, column);
            grdSystemStructure.CurrentCell = new DataGridCellInfo(selItem, column);
            grdSystemStructure.BeginEdit();
            grdSystemStructure.Focus();

Any help would be appreciated.

You have to first set the current cell of your data grid and then call the BeginEdit method to get the keyboard focus inside the cell.

dataGrid1.Focus();
DataGridCellInfo cellInfo = new DataGridCellInfo(itemCollection2.First(), dataGrid1.Columns[0]);
dataGrid1.CurrentCell = cellInfo;
dataGrid1.ScrollIntoView(itemCollection2.First());
dataGrid1.BeginEdit();

I have tested this and it works, set the focus to your grid first after refreshing the grid items.

PS: Please follow the sequence of the method calls as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM