I have a WPF DataGrid. I read a csv file and build an ObservableCollection of objects. I set the DataGrid.ItemsSource to the Collection. I would like to then force a RowValidation on every row in the DataGrid. If I, playing user, edit a cell, the RowValidation fires, all is well. But the Validation does not fire on the initial load. Is there some way I can call ??ValidateRow?? on a row? on every row? (C#, WPF, VS2008, etc)
Asked
Active
Viewed 8,323 times
7
-
You can try `row.BindingGroup.ValidateWithoutUpdate()` https://stackoverflow.com/a/42476772/2122718 – marbel82 Sep 16 '20 at 13:30
2 Answers
2
For your bindings, set the UpdateSourceTrigger to property changed, and then put your validation rules inside that. The default update source trigger is Lost Focus.
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
Also, for another good solution, have a look here
http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
Paul builds a custom error provider, like
You can call the Validate() method on the ErrorProvider to force validation, and check if the controls are valid

amazedsaint
- 7,642
- 7
- 54
- 83
1
You will need to setup a RowValidationRule and set the ValidationStep="ConvertedProposedValue" if you want it to validate after initial loading of the ObservableCollection
<DataGrid Name="dgCsvObjects"
ItemsSource="{Binding Path=CsvObjects}"
AutoGenerateColumns="False">
<DataGrid.RowValidationRules>
<Validation:MyObjectValidationRule ValidationStep="ConvertedProposedValue" />
</DataGrid.RowValidationRules>
<DataGrid.Columns>
<DataGridTextColumn Header="Property1" Binding="{Binding Path=Property1}" />
<DataGridTextColumn Header="Property2" Binding="{Binding Path=Property2}" />
<DataGridTextColumn Header="Property3" Binding="{Binding Path=Property3}" />
<DataGridTextColumn Header="Property4" Binding="{Binding Path=Property4}" />
</DataGrid.Columns>
</DataGrid>

Khan
- 17,904
- 5
- 47
- 59