Reputation: 1270
In my application I have a model and viewmodel which implement IDataError this all works but for example when I open view for adding new customer if my validation rule requires First and Last name not to be null or empty those validations are immediately evaluated and user sees form with errors asking them to enter those data. how can I just show clean entry form but still show validation when property changes or input lost focus?
Upvotes: 2
Views: 1052
Reputation: 50672
In this scenario the validation you implemented is wrong. While in other situations a LastName property cannot be empty in this scenario it is allowed.
What is not allowed, is saving a Customer with empty fields.
So you have to adjust your validation in this ViewModel accordingly.
To have input validation the way you describe it (lost focus) is impossible if you want to give the user the freedom to enter the fields in random order.
I see two acceptable ways:
Upvotes: 0
Reputation: 22445
first if your rule say that first and lastname should not be empty - its right that the user see the validation error.
what i have done is to use a ValidationTemplate for empty values, so that the user just see a "*" for requiered field.
<ControlTemplate x:Key="ValidationTemplateEmpty" >
<DockPanel>
<TextBlock Text="*" Margin="0,0,3,0" Foreground="Red" Visibility="{Binding ElementName=MyAdornedElement,Path=AdornedElement.Visibility}"
ToolTip="{Binding ElementName=MyAdornedElement,Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"/>
<AdornedElementPlaceholder Name="MyAdornedElement" />
</DockPanel>
</ControlTemplate>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationTemplate}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
<Setter Property="Background" Value="{StaticResource BrushErrorLight}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Validation.HasError" Value="true"/>
<Condition Property="Text" Value=""/>
</MultiTrigger.Conditions>
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationTemplateEmpty}"/>
<Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
Upvotes: 1
Reputation: 2417
If you implements IDataErrorInfo
why do you use validation rule?
Upvotes: 1