Click here to Skip to main content
13,096,858 members (85,578 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Please tell me how to bind the PasswordBox with wpf in MVVM
Posted 14-Jan-11 3:01am
Updated 24-May-17 17:52pm
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 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 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 1

You could start with this[^] article.
  Permalink  
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>
</interactivity:interaction.triggers>
</button>
/*
xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
*/


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 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
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 11

  Permalink  
Comments
CHill60 25-May-17 6:57am
   
The question is over 6 years old and already resolved.
pitambar sahoo 4-Jun-17 1:55am
   
Can you please share the link , where I can get the answer.
CHill60 4-Jun-17 8:40am
   
There are several links posted in the solutions above! Someone using an appropriate search should find the MS document you refer to sooner than any of the solutions here.
If you need to find the answer why did you post a solution? (That is rhetorical by the way)

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 |
Web03 | 2.8.170813.1 | Last Updated 24 May 2017
Copyright © CodeProject, 1999-2017
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