Click here to Skip to main content
12,450,937 members (49,621 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF C#4.0 MVVM
Please tell me how to bind the PasswordBox with wpf in MVVM
Posted 14-Jan-11 3:01am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

For security reason WPF didn't gave any Dependency property in Password box. How ever if you really want to achieve what you want then you can follow Samual Jack's[^] blog post where he gave an elegant solution.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

Use the below attached property to bind password box

<pre>public class BoundPasswordBox
    {
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(BoundPasswordBox),
            new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
 
        public static readonly DependencyProperty AttachProperty =DependencyProperty.RegisterAttached("Attach",typeof(bool), typeof(BoundPasswordBox), 
            new PropertyMetadata(false, Attach));
 
        private static readonly DependencyProperty IsUpdatingProperty =DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
           typeof(BoundPasswordBox));
 

        public static void SetAttach(DependencyObject dp, bool value)
        {
            dp.SetValue(AttachProperty, value);
        }
 
        public static bool GetAttach(DependencyObject dp)
        {
            return (bool)dp.GetValue(AttachProperty);
        }
 
        public static string GetPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(PasswordProperty);
        }
 
        public static void SetPassword(DependencyObject dp, string value)
        {
            dp.SetValue(PasswordProperty, value);
        }
 
        private static bool GetIsUpdating(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsUpdatingProperty);
        }
 
        private static void SetIsUpdating(DependencyObject dp, bool value)
        {
            dp.SetValue(IsUpdatingProperty, value);
        }
 
        private static void OnPasswordPropertyChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
            passwordBox.PasswordChanged -= PasswordChanged;
 
            if (!(bool)GetIsUpdating(passwordBox))
            {
                passwordBox.Password = (string)e.NewValue;
            }
            passwordBox.PasswordChanged += PasswordChanged;
        }
 
        private static void Attach(DependencyObject sender,DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
 
            if (passwordBox == null)
                return;
 
            if ((bool)e.OldValue)
            {
                passwordBox.PasswordChanged -= PasswordChanged;
            }
 
            if ((bool)e.NewValue)
            {
                passwordBox.PasswordChanged += PasswordChanged;
            }
        }
 
        private static void PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
            SetIsUpdating(passwordBox, true);
            SetPassword(passwordBox, passwordBox.Password);
            SetIsUpdating(passwordBox, false);
        }
 
    }
Add the namespace of attached property ("here util:") and use like below:
<passwordbox x:name="TxtPassword" util:boundpasswordbox.attach="True" util:boundpasswordbox.password="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" xmlns:util="#unknown" xmlns:x="#unknown">
</passwordbox>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The problem is that with PasswordBox there is no Idependency property :S
  Permalink  
Comments
Abhinav S 14-Jan-11 9:19am
   
Sorry, try this - http://www.wpftutorial.net/PasswordBox.html. You need to use an attached behaviour.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

Why not Pass the whole Password object to ViewModel.I believe there is also a login or submit button. pass the passwordbox object via the submit button(as commandparameter).
<passwordbox name="password" />
<button>
<interactivity:interaction.triggers xmlns:interactivity="#unknown">
<interactivity:eventtrigger eventname="Click">
<cmd:eventtocommand command="{Binding SignIn}" commandparameter="{Binding ElementName=password}" xmlns:cmd="#unknown" />
</interactivity:eventtrigger>

Then cast it to Passwordbox object in ViewModel
private void SigningIn(object parameter)
{
PasswordBox PasswordObj= parameter as PasswordBox;
string Password = password.Password;//here is the entered password
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

In your View:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
....

<PasswordBox Grid.Column="1" PasswordChar="*" x:Name="pwbox"     Password="test"   >
                      <i:Interaction.Triggers>
                          <i:EventTrigger EventName="PasswordChanged" >
                              <i:InvokeCommandAction Command="{Binding PasswordChangedCommand }"  CommandParameter="{Binding ElementName=pwbox,  Mode=OneWay}"/>
                          </i:EventTrigger>
                      </i:Interaction.Triggers>
                  </PasswordBox>




In the ViewModel(wich is DataContext of your View):

public ICommand PasswordChangedCommand
    {
        get
        {
            return new RelayCommand<object>(ExecChangePassword);
        }
    }
 
  
 
    private void ExecChangePassword(object obj)
    {
        yourPassword = ((System.Windows.Controls.PasswordBox)obj).Password;
    }


Some other class...
public class RelayCommand<T> : ICommand
{
 
 
 
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<T> execute)
        : this(execute, null)
    {
    }
      public void Execute(Object parameter)
        {
            _execute((T)parameter);
        }}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You could start with this[^] article.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

I spent ages trying to get this working. In the end, I gave up and just used the DevExpress PasswordBoxEdit. It allows binding.

https://www.devexpress.com/Support/Center/Question/Details/Q506351[^]
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 21 Aug 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100