I have always admired the way Josh Smith has built his sample application. And I have also tried to emulate the way in which the ViewModels of his application implements the IDataErrorInfo property and through a custom DataTemplate renders the errors before the user. Here is the data template that he uses to show the error:
<DataTemplate DataType="{x:Type ValidationError}">
<TextBlock FontSize="10"
FontStyle="Italic"
Foreground="Red"
HorizontalAlignment="Right"
Margin="0,1"
Text="{Binding Path=ErrorContent}"/>
</DataTemplate>
A working sample of this data template getting consumed is as follows:
<TextBox x:Name="txtUsername"
Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
Width="300"
Margin="2"
Text="{Binding Path=Username,
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{x:Null}"/>
<ContentPresenter Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
Content="{Binding ElementName=txtUsername,
Path=(Validation.Errors).CurrentItem}"/>
The default ErrorTemplate of the textbox (a red boundary that appears around it) is replaced by a new error template in which a content presenter placed just below the text box will convey the error to the user — certainly a superior and more elegant template.
If you have read the above code, you might well have guessed that I am trying to create a login form.
Unfortunately, login forms demand password (and subsequently PasswordBox). PasswordBox does not provide "Password" as dependency property. I did not want to break the MVVM guideline of trying to avoid code behind as much as possible and so was tempted to go for the PasswordBoxAssistant class mentioned here. This otherwise is a nice solution, save one thing. It is not letting me validate the Password Box with Josh's data template. I have validated the password property of my ViewModel for not being empty. The property is getting validated as my "Login" button is not getting enabled without the user filling in password. But the "Enter password" message that I did set as a part of this property validation is not being rendered by the Content presenter that lies below the PasswordBox. Here goes the code:
<Label Content="Password:" Grid.Column="0" Grid.Row="2" Margin="2" />
<PasswordBox x:Name="PasswordBox"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"
Margin="2"
Validation.ErrorTemplate="{x:Null}"
ff:PasswordBoxAssistant.BindPassword="true"
ff:PasswordBoxAssistant.BoundPassword="{Binding Path=Password,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
<ContentPresenter Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
Content="{Binding ElementName=PasswordBox,
Path=(Validation.Errors).CurrentItem}"/>
Needless to say that the ff in the above code stands for the namespace reference:
xmlns:ff="clr-namespace:MyProject.UserViews"
I am sure, this problem is happening because the Password property has got extended by the helper class. If I drop this approach, I will have to remove the Password property from the IDataErrorInfo Implementation and on Login button click will have to verify it, presenting the user with a message box. But not without compromising consistency. I am not much aware of Dependency Properties; can there be any workaround? Will altering the helper class in some way let me have the red error message back in place?