PasswordBox validation

Feb 17, 2011 at 7:26 PM
Edited Feb 17, 2011 at 7:28 PM

The Password property of the PasswordBox element is not a DependencyObject so cannot be bound (there is no Text property).  Capturing the password as the user types is possible by attaching a message to the PasswordChanged event, passing the source and grabbing it in the view model.  However the lack of binding means there no opportunity to set ValidatesOnDataErrors=True so the IDataErrorInfo methods are never call meaning any error template is never activated.  Anyone know how to resolve this? That is, is there another way to have an element invoke a call to the view model for the IDataErrorInfo indexer?

One legitimate question might be why would you want to validate a password.  Two reasons:  1) To let the user know the password is required; 2) to let the user know if the password and password confirmation are the same.

Feb 18, 2011 at 10:05 AM

Hi, I hit this problem.  I used <- that article to get round it.

I used the Filters recipie and the Preview/Depdendency filters to enable the Login Button.

I'm sure this can be adapted.

Feb 18, 2011 at 10:27 AM

Thanks for the thought. 

As I tried to describe in the post I don't have a problem accessing the password - setting a Message.Attach on the PasswordBox to respond to the PasswordChange event provides a method to access the the password from the view model and much more simply than using an attached property.  So I'm able to enable/disable a button.  However I can't explain to the user *why* the button is enabled/disabled.  Because there's no actual binding taking place (the source Password property is not a dependency property) the Validation.HasErrors of the view model's IDataErrorInfo is never called.

The same problem exists with the approach in the article you reference as it too is not really binding the underlying Password property (I originally tried the functionalfun example on which the article is based).

So the core problem remains: how can the error template be activated in response to some other event or method action?

Feb 18, 2011 at 1:46 PM

In your guard method, if it evaluates to false, you could set some boolean property on your view model indicating that there was an error. In the view, you could bind an element's visibility to this boolean in order to alert the user. It's a bit specific, but it should work just fine.

Feb 18, 2011 at 2:35 PM

Yes, I could but that would not be consistent with the way validation errors are reported for every other control in the application and I'd like to find a solution. I know this is nothing to do with Caliburn but someone here might have a bright idea! Or maybe a TextBox implementation that allows a PasswordChar to be set.

Feb 18, 2011 at 2:51 PM

In response to this and you other question: You could create a custom attached porperty, which could use a binding expression to your VM's password property. Under the covers it could update the actual Password control on the fly. You could probably wire something up so that it setts the validation related attached properties as apprpriate. I have *not* done this, but I think it would work. You just might have to experiement a bit.

Feb 18, 2011 at 3:01 PM

<<You could create a custom attached porperty...>>

I've done that using the example shown at the link in post two of this thread.  As I tried to decribe in the request for information about how to use Message.Attach() with attached properties, the problem is that although the attached property binds successfully to another element in the view, it doesn't appear to bind to a view model property.  At least the view model property setter is never executed.  Perhaps because the view model inherits from Screen not Dependency object?

Feb 18, 2011 at 3:14 PM

No...there's something else going on there. Not sure what.

Feb 18, 2011 at 4:33 PM


OK, I've been able to implement the solution the way I want and using nothing more complicated than a only using a Message.Attach.  No screwing around with attached properties.  I'll update the validation recipe to include this example as I'd like others to have opportunity of not enduring this learning cyle.

Feb 18, 2011 at 4:46 PM

I'm interested to see what ou came up with as well :)

Jan 31, 2012 at 5:33 AM

@bseddon or @EisenbergEffect

I'm interested in understanding the solution that bseddon came up with using Message.Attach to retrieve the contents of a PasswordBox.  Can you confirm where the "validation recipe" is located?  I've been searching around and not able to find anything.


Jan 31, 2012 at 10:34 AM

Yes, it's a little awkward.  This apparent obfuscation is necessary because the Password property of WPF PasswordBox control is not a dependency property so cannot be bound to the view model by Caliburn.  

To work around this limitation the Caliburn Message.Attach() feature is used to call the PasswordChanged method on the view model when the change event is fired.  The password control is passed to the PasswordChanged method on the view model using the $source variable.  Because the method is passed a reference to the password control, it is able to read the password directly and assign it to the Password property of the view model.  It also means that one PasswordChanged method can handle multiple passwords by assigning the control's password based on the control instance passed to the method.

Just like a normally bound view model property and control, the act of assigning the password value causes the validation mechanism to be initiated.

Put a break point the PasswordChanged method then change the password and you'll see what's going on.

Jun 3, 2015 at 9:42 PM
Edited Jun 3, 2015 at 9:43 PM
Please - where is the "validation recipe" is located?