
Validation in the XamDataGrid is currently handled through the ValueConstraint class off of the Editors. Using this class you can do things like set a trigger to change the border of a cell to red when an invalid value it entered. This article shows off how to use the ValueConstraints class.
The first thing to understand is that there are multiple types of constraints and they include the following:
- FixedValue Condition – Does the value equal this?
- Enumeration Condition – An Object implementing IEnumerable which contains a list of values.
- MaxExclusive Condition – The value must be less than this.
- MaxInclusive Condition – The max value that the constrained value can be.
- MinExclusive Condition – The value must be greater than this.
- MinInclusive Condition – The min value that the constrained value can be.
- Min/MaxLength Condition – The length of type string must be greater/less than this.
- Nullable Condition – Whether the constrained value can be null.
- RegExPattern Condition – A regular expression for the value.
Using BindToSampleData property on the XamDataGrid to practice validation.
Enumeration of Fixed Values:
<igDP:Field Name="department" Label="Department">
<igDP:Field.Settings>
<igDP:FieldSettings EditorType="{x:Type igEdit:XamTextEditor}">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEdit:ValueEditor}">
<Style.Resources>
<x:Array Type="sys:String" x:Key="FixedValue">
<sys:String>Admin</sys:String>
<sys:String>Sales</sys:String>
<sys:String>Human Resources</sys:String>
</x:Array>
</Style.Resources>
<Style.Setters>
<Setter Property="ValueConstraint">
<Setter.Value>
<igEdit:ValueConstraint Enumeration="{StaticResource FixedValue}" />
</Setter.Value>
</Setter>
<Setter Property="InvalidValueBehavior" Value="RetainValue" />
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsValueValid" Value="false">
<Trigger.Setters>
<Setter Property="BorderBrush" Value="Red" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
Max/Min Inclusive:
<igDP:Field Name="salary" Label="Salary">
<igDP:Field.Settings>
<igDP:FieldSettings EditorType="{x:Type igEdit:XamCurrencyEditor}" EditAsType="{x:Type sys:Double}">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEdit:ValueEditor}">
<Style.Setters>
<Setter Property="ValueConstraint">
<Setter.Value>
<igEdit:ValueConstraint MinInclusive="0" MaxInclusive="100000" />
</Setter.Value>
</Setter>
<Setter Property="InvalidValueBehavior" Value="RetainValue" />
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsValueValid" Value="false">
<Trigger.Setters>
<Setter Property="BorderBrush" Value="Red" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
Reg-Ex for an E-Mail:
<igDP:Field Name="email" Label="E-Mail">
<igDP:Field.Settings>
<igDP:FieldSettings EditorType="{x:Type igEdit:XamTextEditor}">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEdit:ValueEditor}">
<Style.Setters>
<Setter Property="ValueConstraint">
<Setter.Value>
<igEdit:ValueConstraint RegexPattern="^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$" />
</Setter.Value>
</Setter>
<Setter Property="InvalidValueBehavior" Value="RetainValue" />
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsValueValid" Value="false">
<Trigger.Setters>
<Setter Property="BorderBrush" Value="Red" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
EditModeValidationError Event
So, your user has failed the validation logic that you’ve put in place. When this happens the EditModeValidationError Event fires and allows you to perform some action. For instance, you can configure the error message as follows:
private void xamDataGrid1_EditModeValidationError(object sender, Infragistics.Windows.DataPresenter.Events.EditModeValidationErrorEventArgs e)
{
switch (e.Cell.Field.Name.ToString())
{
case "department":
e.ErrorMessage = "That department does not exist! Please enter a valid department."
break;
case "salary":
e.ErrorMessage = "Salary cannot be more than $100,000.00";
break;
case "email":
e.ErrorMessage = "Please enter an e-mail address (e.g. __@__.com)";
break;
}
}
Conclusion
That wraps up this article on validation in the XamDataGrid. Now keep in mind that this works with all of our editors as well; so you’re not…constrained…to the XamDataGrid to use these capabilities. You can grab the code for this sample here.
