In my xaml, i use a lot of binding validationRules
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content"> <Binding.ValidationRules> <validations:ValidateNotNullOrWhiteSpace ValidatesOnTargetUpdated="True" /> </Binding.ValidationRules> </Binding>
Now I have a button that launch some calculation and I have a function that show a messagebox if there is validation error in the form.
To check if the form isValid, I use the native BindingValidator and IsValid.
private IBindingValidator _bindingValidator = new BindingValidator();..._bindingValidator.IsValid(this)My problem is that the validation rule of the grid are not working with this and the IsValid function return true.
Is there any way to know if the whole grid is valid with XamDataGrid
Thank you,
Charles
Hello Charles,
My team and I have been reviewing this forum thread, and I have a couple of questions for you in this case. Would it be possible for you to please provide some information regarding the following?
With the above questions, I cannot say for absolute certain if those Binding.ValidationRules will work in the XamDataGrid – and I highly doubt they will work if placed directly on the grid itself. You would likely need to do this on a cell.
With the above said, I feel you might be better suited to potentially using the IDataErrorInfo interface, as the XamDataGrid supports this out-of-the-box, and this may help you to find out if the grid is valid. You can read further about the support of the IDataErrorInfo interface for the XamDataGrid here: https://www.infragistics.com/help/wpf/xamdatapresenter-validating-data.
Please let me know if you have any other questions or concerns on this matter.
The binding is directly in my XamDatagrid.
We use TemplateField and TemplateField.DisplayTemplate.
The validation work and each cell not valid is highlighted in red, but we need a way to check if all the cells are valid.
<igDP:TemplateField Name="SheetsGauge" AllowEdit="False" Width="50" AllowHiding="Never" Label="{x:Static resx:Labels.VerifFormDeckMultiple_SheetsGridGauge}" BindingType="UseAlternateBinding" AlternateBinding="{Binding Gauge}" ToolTip="{x:Static resx:Tooltips.VerifFormDeckMultiple_SheetsGridGauge}"> <igDP:TemplateField.CellValuePresenterStyle> <Style TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="ToolTip"> <Setter.Value> <TextBlock Text="{Binding DataItem.FullDescription}"/> </Setter.Value> </Setter> </Style> </igDP:TemplateField.CellValuePresenterStyle> <igDP:TemplateField.DisplayTemplate> <ItemContainerTemplate> <TextBox x:Name="txtSheetsGauge" IsReadOnly="True" Margin="1,1,1,1" Background="Transparent" BorderBrush="Transparent"> <TextBox.Text> <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content"> <Binding.ValidationRules> <validations:ValidateNotNullOrWhiteSpace ValidatesOnTargetUpdated="True" /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </ItemContainerTemplate> </igDP:TemplateField.DisplayTemplate> </igDP:TemplateField>
Thank you for your update on this matter. I have been experimenting on my end with a sample project to see if I can find a good way to tell if the entire grid is valid using this option, but it does not seem like this is a good way of going about things.
The reason I say this, is because when one of the Binding.ValidationRules is invalid, it updates the attached Validation.HasError and Validation.Errors properties on the element that is invalid. In this case, it would be on the TextBox and not the grid itself. The problem with this now lies in the virtualization of the grid as the containers that are outside of the currently “in view” records and cells will be null as they are virtualized, and so these TextBox elements will not actually exist as they exist as part of the CellValuePresenter container that is virtualized when out of view. If no cell that has an error is in view, you won’t be able to tell if there is an error or not in this way without shutting off the virtualization of the grid, which is a bad idea with larger data sets as it will create all of the containers for the records and cells on startup, leading to a potentially huge performance hit.
With that said, if you do want to continue this way, you can tell if the currently in view cells are valid for your TemplateField by looping through the Records in the Record[] returned by the grid’s GetRecordsInView method. You can then get the cell belonging to your TemplateField from the record’s Cells collection. At this point, you can get the CellValuePresenter visual element from that Cell using the static CellValuePresenter.FromCell method. Finally, since you have an x:Name on your TextBox, you can use the Infragistics.Windows.Utilities class and its static GetDescendantFromName method to get it from the CellValuePresenter element and check the attached property Validation.HasError on it. The code for this would look something like the following, where “xdg1” is the XamDataGrid:
Record[] records = xdg1.GetRecordsInView(true); foreach(Record record in records) { if(record is DataRecord) { Cell cell = ((DataRecord)record).Cells["NameOfTemplateField"]; CellValuePresenter cvp = CellValuePresenter.FromCell(cell); TextBox text = Infragistics.Windows.Utilities.GetDescendantFromName(cvp, "NameOfTextBox") as TextBox; bool hasError = Validation.GetHasError(text); } }
In this case, I will again recommend usage of the IDataErrorInfo interface, as this will happen on your underlying data items rather than the grid itself. This will let you query your data source for a data item that has an error to determine whether or not the grid is actually valid and will not be determinant on what values are in view.