Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 MVVM
Please tell me how to bind the PasswordBox with wpf in MVVM
Posted 14-Jan-11 4: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));
 
<pre>
    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);
    }
 
}</pre></pre>

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 at 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>
</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 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
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 28 Oct 2014
Copyright © CodeProject, 1999-2015
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