Pregunta

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?

¿Fue útil?

Solución

I don't see ValidatesOnDataErrors=True in your Password binding, so perhaps that is your problem. By default, that is set to False which means the binding won't alert the UI of any validation errors.


That said, I think the Password is intentionally not a DependencyProperty because you really shouldn't be storing passwords as plain text.

Usually I end up passing the PasswordBox.Password (or the entire PasswordBox) as a CommandParameter to my LoginCommand, then it can take the data and do whatever it wants to it. Usually this means hashing it or something and comparing it with the stored password's hash to see if its the same. If the login fails, I write the associated error message to a property in my ViewModel, which is bound to the login UI.

<Button Command="{Binding LoginCommand}" 
        CommandParameter="{Binding ElementName=MyPasswordBox, Path=Password}" />

Otros consejos

Not sure if this is your problem, but you're not turning off the default ErrorTemplate for the PasswordBox. (ie no Validation.ErrorTemplate="{x:Null})

EDIT: Can you use something like Wpf Inspector to check the content of Validation.Errors of the PasswordBox to ensure that the error is actually there (or even just bind an ItemsControl to it) ?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top