WPF / SIlverlight / Phone - how to access password from PasswordBox control

by Administrator 22. November 2011 19:41

I was glad to see that Silverlight 4 and Windows Phone 7.1 now support the PasswordBox; this permits my login view for my Multi Targeted application Solr Contrib to share the same xaml (which I have linked).  At the time of this writing Visual Studio 2011 does not support Windows phone so for now I have to limit my shared code to Desktop and Silverlight - when phone is supported I'll be able to plug these new features into my open source password manager (http://PasswordMgr.CodePlex.com).

If you attempt to use data binding to gain access to the Password property you'll find that the compiler will complain.  You'll have to get a reference to the PasswordBox control (txtPassword below) and reference its Password property to get the users input.

One of the many powerful features of MVPVM is the ability to access the View as well as the ViewModel from the presenter.   This allows us to easily access the txtPassword.Password property, as shown below on line 35.

I placed a breakpoint on line 39 and see that I have access to both my login/password and can send it to my business logic layer's ValidateUser method to see if the user is authenticated.   

Source code available at http://SolrContrib.CodePlex.com Changeset 94368