I want datagrid to continue Adding or Editing DataGrid row even if row has error validation rule
this is sample in order to clarify the idea
<DataGrid Name="Dgrid" AutoGenerateColumns="False" ItemsSource="{Binding Request.Obligations}" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" />
<DataGridTextColumn Binding="{Binding Name}"/>
<DataGridTextColumn Binding="{Binding Age}"/>
</DataGrid.Columns>
<DataGrid.RowValidationRules>
<validationRules:ٌRowValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<!--<Grid Margin="0,-2,0,-2"
ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},Path=(Validation.Errors)[0].ErrorContent}">-->
<Grid Margin="0,-2,0,-2">
<Grid.ToolTip>
<MultiBinding Converter="{StaticResource DataGridRowMultiConverter}">
<Binding Path="(Validation.Errors)[0].ErrorContent"
RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType=DataGridRow}" />
<Binding Path="DataContext"
RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType=Window}" />
</MultiBinding>
</Grid.ToolTip>
<Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
</DataGrid>
If a user entered a new row with invalid data, he will not be able to add a new row and the new row will be missing and the existing data can not be edited.
In short, I want to stop the behavior of RowValidationRules and make its jop to tell the user that he has entered invalid data without stoping him.
After search i found this code:
protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e)
{
var hasCellValidationError = false;
var hasRowValidationError = false;
const BindingFlags bindingFlags =
BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
var cellError = this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags);
var rowError = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags);
if (cellError != null)
hasCellValidationError = (bool)cellError.GetValue(this, null);
if (rowError != null)
hasRowValidationError = (bool)rowError.GetValue(this, null);
base.OnCanExecuteBeginEdit(e);
if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError))
{
e.CanExecute = true;
e.Handled = true;
}
}
but I found that I can edit rows but can't add more rows
What I have tried:
continue Adding or Editing DataGrid row even if row has error validation rule