Hi, I have a list of objects that I'm binding to in my grid, and I'm wondering how to go about validating the input. Each object has some validation rules associated with them, and if I bind a single object to some text boxes, like
<TextBox Text="{Binding Path=PostalCode, ValidatesOnDataErrors=True}"/>
The default behavior in WPF is the textbox will have a red border. I'm wondering how to duplicate that behavior in the grid.
Is this possible?
Hi,
Value editors used by the grid has built-in support for specifying value constraints. However it doesn't have any default appearance when the value constraints fail. To specify some type of visual indication, you can specify an editor style that triggers a visual indication via the editor's IsValueValid property. IsValueValid property basically returns whether the current value is valid based on value constraints.
Here's a xaml snippet that changes the border of the editor to be red when the value is invalid. The snippet also sets the ValueConstraint property of the editor to constraint the value to be between 0 and 100 inclusive.
<dg:XamDataGrid.FieldLayouts>
<dg:Field Name="MyField" >
<dg:FieldSettings EditorType="{x:Type dge:XamTextEditor}">
<Style TargetType="{x:Type dge:ValueEditor}">
<Setter Property="ValueConstraint">
<dge:ValueConstraint MinInclusive="0" MaxInclusive="100" />
</Setter>
</Style.Setters>
<Trigger Property="IsValueValid" Value="false">
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style>
</dg:FieldSettings>
</dg:FieldLayout.Fields>
</dg:XamDataGrid.FieldLayouts>
</dg:XamDataGrid>
Hope this helps,
Sandip
Just some feedback on this: the red border remains when editing/inserting is cancelled, and worse still, the row visuals get recycled and so keep appearing as you scroll!
The answer is to use a MultiTrigger, with conditions 'IsValueValid=false' and crucially 'IsInEditMode=true'.
John
The InvalidValueBehavior seems to always Display a error message when used in the grid.
Sandips example above clearly states "RetainValue" but if I run it on my machine I still get a standard invalid error message!
What should I do to not display the Invalid value MessageBox? or even better, how do I display a custom message?
Ah! so there is also an InvalidValueBehavior property on Field.Settings, which is the one that counts.
confusing...