-2

I Am Using the

textboxValue.SetBinding(TextBox.TextProperty new Binding(strValueBinding));

this Syntax For the TextBox Dynamic Binding Its Working.. Similarly I will Try to PasswordBox Dynamic Binding in Wpf code Behind Dynamic Why But Not Working ...

passwordBox.SetBinding(PasswordBox.DataContextProperty, new Binding(strValueBinding));
                    passwordBox.SetBinding(PasswordBox.PasswordCharProperty, new Binding(strValueBinding));

i Can Try both But Binding Issues is Didnt Solve Any One Know The Dynamic Binding PasswordBox in wpf C#

Gupta
  • 314
  • 2
  • 8
  • [This](https://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm) is what you're looking for. – dotNET Jan 04 '18 at 06:32
  • @dotNET I looking For Dynamic Binding at Code Behind Of the Wpf for the PasswordBox ...................... with the help of the c# – Gupta Jan 05 '18 at 04:47

2 Answers2

1

You cannot set the binding for PasswordBox due to security limitation as per MSDN https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ca97b60-2d8e-4a27-8c5b-b8d5d7370a5e/unable-to-databind-to-a-passwordbox?forum=wpf

PasswordBox binding can be achieved via MVVM How to bind to a PasswordBox in MVVM

hope this helps!

Jsharma

JSH
  • 119
  • 6
1

Ican Use This Class And Bind To PasswordBox Its Working.............

public static class Secure
{
    private static readonly DependencyProperty PasswordInitializedProperty =
        DependencyProperty.RegisterAttached("PasswordInitialized", typeof(bool), typeof(Secure), new PropertyMetadata(false));

    private static readonly DependencyProperty SettingPasswordProperty =
        DependencyProperty.RegisterAttached("SettingPassword", typeof(bool), typeof(Secure), new PropertyMetadata(false));

    public static string GetPassword(DependencyObject obj)
    {
        return (string)obj.GetValue(PasswordProperty);
    }
    private static string currentPassword = string.Empty;
    public static void SetPassword(DependencyObject obj, string value)
    {
        obj.SetValue(PasswordProperty, value);
    }
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password", typeof(string), typeof(Secure),
            new FrameworkPropertyMetadata(Guid.NewGuid().ToString(), HandleBoundPasswordChanged)
            {
                BindsTwoWayByDefault = true,
                IsNotDataBindable=false,
                DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus 
            });

    private static void HandleBoundPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
    {
        var passwordBox = dp as PasswordBox;
        if (passwordBox == null)
            return;


        if ((bool)passwordBox.GetValue(SettingPasswordProperty))
            return;


        if (!(bool)passwordBox.GetValue(PasswordInitializedProperty))
        {
            passwordBox.SetValue(PasswordInitializedProperty, true);
            passwordBox.PasswordChanged += HandlePasswordChanged;
        }

        passwordBox.Password = e.NewValue as string;
    }

    private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
    {
        var passwordBox = (PasswordBox)sender;
        passwordBox.SetValue(SettingPasswordProperty, true);
        SetPassword(passwordBox, passwordBox.Password);
        passwordBox.SetValue(SettingPasswordProperty, false);
    }
}

and After That We Use the Set Binding at code Behind by using c# For the Dynamic Binding... in wpf

PasswordBox passwordBox = new PasswordBox() { Height =30, Width = 450, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, VerticalAlignment = System.Windows.VerticalAlignment.Top, Name = passwordBox1 };
                    passwordBox.SetBinding(Secure.PasswordProperty, new Binding(strValueBinding) { Mode = BindingMode.TwoWay });

its Working........

Gupta
  • 314
  • 2
  • 8