wpfvalidationdatagrid

WPF DataGrid - Validation Recommendations


We are implementing a WPF business application using MVVM. Currently we are trying to determine the best way to show validation errors for DataGrids. Currently we are trying to do so like this:

1) A View user control contains a datagrid bound to an EntityCollection of Entity Framework objects located in the associated ViewModel

2) The ViewModel implments the IDataErrorInfo interface. In the ViewModel there are the normal properties including a property for implementing IDataErrorInfo.Item

3) The property which implements IDataErrorInfo.Item calls the validation from the underlying Model to determine if there are any validation issues

This works fine for any textbox or combobox on the view - validatiosn are being processed as expected. But with the datagrid we are stumped - no matter what we have tried the validation will not happen. Currently it is defined like this:

<DataGrid x:Name="dgWikiNames" ItemsSource="{Binding Wiki_Names, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnSourceUpdated=True}" AutoGenerateColumns="False">
                        <DataGrid.RowValidationRules>
                            <DataErrorValidationRule ValidationStep="UpdatedValue"/>                            
                        </DataGrid.RowValidationRules>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="First Name">
                                <DataGridTextColumn.Binding>
                                    <Binding Path="First_Name" ValidatesOnDataErrors="True">
                                        <Binding.ValidationRules>
                                            <DataErrorValidationRule/>
                                            <!--<local:CellDataInfoValidationRule ValidationStep="UpdatedValue"/>-->
                                        </Binding.ValidationRules>
                                    </Binding>
                                </DataGridTextColumn.Binding>
                            </DataGridTextColumn>
                            <DataGridTextColumn  Header="Is Primary" Binding="{Binding Primary_Flag}"/>
                        </DataGrid.Columns>
                    </DataGrid>

If anyone has any pointers or recommendations we'd appreciate it. Thanks


Solution

  • Add the following to your binding:

    ValidatesOnDataErrors="True" NotifyOnValidationError="True"