Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to cancel editing in DataGridTemplateColumn when Validation detects an error

$
0
0

Hello,

I have a DataGridTextColumn with a Validation attached to the property binding.  Even when the validation fails, the user can cancel editing by pressing the Escape key.

I have a DataGridTemplateColumn with a DatePicker in the CellEditingTemplate and would like to get the same functionality -- allow the user to cancel editing, regardless of what the validation detects.

There is Validation attached to the data binding, and a LostFocus handler:

var dateValidationRule = new DateValidationRule(); dateValidationRule.PropertyName = bindingPath; dateValidationRule.ValidatesOnTargetUpdated = true; b.ValidationRules.Add(dateValidationRule);

datePickerFactory.AddHandler(LostFocusEvent, new RoutedEventHandler(DatePickerLostFocusHandler));      

I have a KeyUp handler configured in the Style for DataGridCell; in the handler, I'd like to end editing when the Escape key is pressed:

private void DataGridCell_KeyUpHandler(object sender, KeyEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    if (cell != null && e.Key == Key.Escape)
    {
        cell.IsEditing = false;
    }
}

Unfortunately, this doesn't work, and I suspect it is because there is still a validation error detected by the LostFocus handler.
I am also trying this:

if (Validation.GetHasError(datePicker))
{
    var b = datePicker.GetBindingExpression(DatePicker.SelectedDateProperty);
    if (b != null)
    {
        Validation.ClearInvalid(b);
    }
}

Unfortunately, b is always null -- how can I get the correct binding to clear the Validation errors? I think if I can get this last snippet working, I'll be able to cancel editing.

Thanks --



Viewing all articles
Browse latest Browse all 18858

Trending Articles