Hello,
I don't find how to set the validation on dynamic fields.
My project is based on the sample because I need dynamic fields.
https://es.infragistics.com/community/blogs/b/blagunas/posts/xamdatagrid-dynamically-create-and-data-bind-columns-with-an-editor-of-your-choice
My dynamic fields, which are the equivalent of the Period class in this example project, implements INotifypropertyChanged.
In my project, the setter of a dynamic field throw an exception if the value is incorrecte.
such as :
public int MyProperty{ set{ if(value<0) throw new ArgumentException("No negative value) _property=value; RaisePropertyChanged(nameof(MyProperty)); } get{ return _property; } }
I have define in the fieldlayoutSettings of my xamdatagrid:
SupportDataErrorInfo = "RecordsAndCells" and DataErrorDisplayMode="ErrorIconAndHighlight" but nothing appears.
My viewModel doesn't implement any ErrorInfo interface (IDataErroInfo,...).
In debug mode, the exception is thrown but it is never "caught"!! So the field seems Ok whereas the value is incorrect, forbidden,....
I don't understand why nothing is displayed because I followed this example which seems really simple
https://www.infragistics.co.kr/help/wpf/xamgrid-validating-data
Did I miss something?
Thank you.
Thank you a lot, Brian.
I have adapted my application with your recommendations.
That change the ergonomy of the application, but I will adapt the ViewModel.
Thanks, Thanks, Thanks.
Using exceptions for data validation is not recommended. It is highly recommended to use the IDataErrorInfo interface as Michael has demonstrated. I am attaching a sample that uses the IDataErrorInfo and the xamComboEditor and all rules that have been violated are properly represented in the grid cell with the appropriate error messages.1258.XamDataGridDynamicColumns.zip
I'm affraid michael, but that won't work. Nothing appends with your sample.
You call the method SetFieldDataError with the name "Period 1" but in the Period class there is no property named "Period 1".
There is a variable count of Period in the ViewModel StaffMember which are stored in the property Periods of type ObservableCollection<Period>. So I can't do
I understand what you are trying to do. But, the ViewModel of dataitem of the xamdatagrid is not the Period class. It's the StaffMember class (in the code done by brian LAGUNAS).
Then the IDataErrorInfo implementation is in the wrong place, no?
With an instance of ExceptionValidationRule, I can "catch" the exception thrown by the Period.Hour setter.
So in the EditModeEnding, I can check if the editor has error with
Validation.GetHasError(e.Editor).
Then I can set the eventArgs.Cancel to true and display a messageBox with the exception message.
That works for TextEditor and NumericEditor, but if the editor type is a xamcomboeditor that won't work and I don't understand why.
I attached a modified version of your sample with IDataErrorInfo support.
Two key things were added.
1. I updated the FieldLayoutSettings to enable the feature.
eg.
SupportDataErrorInfo="RecordsAndCells" DataErrorDisplayMode="ErrorIconAndHighlight"
2. Inside the setter for Hours, Period.cs, I added validation and IDataErrorInfo similar to how to demonstrate this in our docs.
Let me know if you have any questions.
ModifiedSample.zip
So with an ExceptionValidationRule applyed on bindings, I can display a dialog message in the EditModeEnded event if the Validation.GetHasError(e.Editor) is true.
That works for XamTextEditor. But for XamComboEditor with the property IsEditable = true. That don't work.
Could you explain me how to execute validation rules for editable XamComboEditor?