title: Bind SecurePassword to ViewModel
body: <p>I try to bind the <code>SecurePassword</code> property of a <code>PasswordBox</code> to my <code>ViewModel</code> with a custom <code>Behavior</code>. Sadly it doesn't work properly.</p>
  
  <p>Basically I added a property to the <code>Behavior</code> which contains the target property of my <code>ViewModel</code>.</p>
  
  <p>Any ideas why it doesn't work?</p>
  
  <p>PS: I am currently on the way home without my laptop, I gonna update the question with my code in about 15 minutes. But would be nice if someone would post ideas or sth.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As I promised, here is some code :)</p>
  
  <p>The <code>Behavior</code> first:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  using System.Windows.Interactivity;
  using System.Security;
  
  namespace Knerd.Behaviors {
      public class PasswordChangedBehavior : Behavior&lt;PasswordBox&gt; {
  
          protected override void OnAttached() {
              AssociatedObject.PasswordChanged += AssociatedObject_PasswordChanged;
              base.OnAttached();
          }
  
          private void AssociatedObject_PasswordChanged(object sender, RoutedEventArgs e) {
              if (AssociatedObject.Password != null)
                  TargetPassword = AssociatedObject.SecurePassword;
          }
  
          protected override void OnDetaching() {
              AssociatedObject.PasswordChanged -= AssociatedObject_PasswordChanged;
              base.OnDetaching();
          }
  
          public SecureString TargetPassword {
              get { return (SecureString)GetValue(TargetPasswordProperty); }
              set { SetValue(TargetPasswordProperty, value); }
          }
  
          // Using a DependencyProperty as the backing store for TargetPassword.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty TargetPasswordProperty = DependencyProperty.Register("TargetPassword", typeof(SecureString), typeof(PasswordChangedBehavior), new PropertyMetadata(default(SecureString)));
      }
  }
  </code></pre>
  
  <p>The <code>PasswordBox</code>:</p>
  
  <pre><code>&lt;PasswordBox Grid.Column="1" Grid.Row="1" Margin="5" Width="300" MinWidth="200"&gt;
      &lt;i:Interaction.Behaviors&gt;
          &lt;behaviors:PasswordChangedBehavior TargetPassword="{Binding Password}" /&gt;
      &lt;/i:Interaction.Behaviors&gt;
  &lt;/PasswordBox&gt;
  </code></pre>
  
  <p>And last, the part of my <code>ViewModel</code>.</p>
  
  <pre><code>private SecureString password;
  
  public SecureString Password {
      get { return password; }
      set {
          if (password != value) {
              password = value;
              OnPropertyChanged("Password");
          }
      }
  }
  </code></pre>
  
  <p>I hope anyone can help, atm I use the codebehind version but I rather wouldn't.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>What actually doesn't work is, that the <code>TargetPassword</code> property doesn't update the property of my <code>ViewModel</code></p>