title: Password/Mask field in WPF PropertyGrid is not persisted
body: <p>I'm trying to build a password (masked) field for a WPF PropertyGrid that's used in a WF re-hoster designer solution. Note that I'm not interested in the security element of it. I just want the password hidden from the user. I really struggled with something I would have thought would have been simple to implemented but I eventually found this great article which really helped:</p>
  
  <p><a href="http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html" rel="nofollow noreferrer">WPF PasswordBox and Data binding</a></p>
  
  <p>After creating the PasswordBoxAssistant class as per article, I created a WPF UserControl with the following XAML and Code:</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;Grid&gt;
      &lt;PasswordBox x:Name="PasswordBox"
                   Background="Green" 
                   local:PasswordBoxAssistant.BindPassword="true" 
                   local:PasswordBoxAssistant.BoundPassword="{Binding Password,
                   Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Code-Behind:</strong></p>
  
  <pre><code>public partial class PasswordUserControl : UserControl
  {
      public PasswordUserControl()
      {
          InitializeComponent();
      }
  
      public static readonly DependencyProperty PasswordProperty = 
        DependencyProperty.Register("Password",
        typeof(string), typeof(PasswordUserControl),
        new FrameworkPropertyMetadata(PasswordChanged));
  
      public string Password
      {
          get
          {
              return (string)GetValue(PasswordProperty);
          }
          set
          {
              SetValue(PasswordProperty, value);
          }
      }
  
      private static void PasswordChanged(DependencyObject source, 
         DependencyPropertyChangedEventArgs e)
      {
          if (e.NewValue != null)
          {
              (source as PasswordUserControl)?.UpdatePassword(e.NewValue.ToString());
          }
          else
          {
              (source as PasswordUserControl)?.UpdatePassword(string.Empty);
          }
      }
  
      private void UpdatePassword(string newText)
      {
          PasswordBox.Password = Password;
      }
  }
  </code></pre>
  
  <p>Then I created a <code>PropertyValueEditor</code> class (called PasswordEditor.cs) and I'm assuming this is the part that's not working and I'm not setting correctly. </p>
  
  <p>I'm binding the PasswordProperty from the <code>UserControl</code> to the <code>Value</code> field of the <code>PropertyValueEditor</code>.</p>
  
  <pre><code>public class PasswordEditor : PropertyValueEditor
  {
      public PasswordEditor()
      {
          this.InlineEditorTemplate = new DataTemplate();
          FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel));
          FrameworkElementFactory passwordBox = new 
                      FrameworkElementFactory(typeof(PasswordUserControl));
          Binding passwordBoxBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
          passwordBox.SetValue(PasswordUserControl.PasswordProperty, passwordBoxBinding);
          stack.AppendChild(passwordBox);
          this.InlineEditorTemplate.VisualTree = stack;
      }
  }
  </code></pre>
  
  <p>I've tried to set it to the <code>StringValue</code> as well as per other WF PropertyValueEditor samples I have found but to no avail.</p>
  
  <p>The password (masked) field is now displaying correctly in my WPF PropertyGrid, but it's not persisting the value when I switch to another WF activity and switch back to the one that contains the password field.</p>
  
  <p>Can anyone point me in the right direction?</p>
  
  <p>Thanks.</p>
  
  <p>Again, any help would be appreciated.</p>
  
  <p>Thanks.</p>