title: Using PasswordBox with WPF - MVVM
body: <p>I've read several articles about how to use Attached Properties to bind to the value of a PasswordBox in WPF.  However, every article also references the .NET documentation which explains why the PasswordBox was not made bindable in the first place.</p>
  
  <p>I do not consider myself a security expert by any means, but I figure that someone at Microsoft knew what they were doing, and I shouldn't be putting forth the effort trying to undo it.</p>
  
  <p>So, instead, I came up with my own solution.</p>
  
  <pre><code>public class LoginViewModel
  {
     // other properties here
  
     public PasswordBox Password
     {
        get { return m_passwordBox; }
     }
  
     // Executed when the Login button is clicked.
     private void LoginExecute()
     {
        var password = Password.SecurePassword;
  
        // do more stuff...
     }
  }
  </code></pre>
  
  <p>Then, in my XAML, I just render the PasswordBox by binding the Password field to a <code>ContentPresenter</code>.</p>
  
  <p>So my question is... is there a problem with doing it this way?  I realize that I'm sort of breaking the MVVM in a way by letting actual controls appear in my ViewModel, but at least this seems more correct than just un-securing the password field.</p>
  
  <p>If this is, in fact, a problem, has anyone come up with a solution that doesn't involve using Attached Properties and storing the password in the ViewModel?</p>
  
  <p>Thanks!
  -J</p>