passwordBox.SetBinding(PasswordBox .........,new Binding(strValueBinding))Wpf后面的代码c#

时间:2018-01-04 05:39:17

标签: c# wpf

我正在使用

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

这个语法用于TextBox动态绑定其工作.. 类似地,我将尝试在Wpf代码中使用PasswordBox动态绑定动态为什么但不工作......

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

我可以尝试两种但是绑定问题并没有解决 任何人都知道wpf C#中的动态绑定PasswordBox

2 个答案:

答案 0 :(得分:1)

由于MSDN https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ca97b60-2d8e-4a27-8c5b-b8d5d7370a5e/unable-to-databind-to-a-passwordbox?forum=wpf

的安全限制,您无法为PasswordBox设置绑定

PasswordBox绑定可以通过MVVM实现 How to bind to a PasswordBox in MVVM

希望这有帮助!

Jsharma

答案 1 :(得分:1)

我可以使用这个类并绑定到PasswordBox它的工作.............

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);
    }
}

之后我们使用c#在代码背后使用Set Binding用于动态绑定...在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 });

它的工作........