Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wpfc后面的SetBinding(passwordBox………,新绑定(strValueBinding))代码#_C#_Wpf - Fatal编程技术网

C# wpfc后面的SetBinding(passwordBox………,新绑定(strValueBinding))代码#

C# wpfc后面的SetBinding(passwordBox………,新绑定(strValueBinding))代码#,c#,wpf,C#,Wpf,我正在使用 textboxValue.SetBinding(TextBox.TextProperty new Binding(strValueBinding)); 此语法用于文本框动态绑定其工作状态。。 类似地,我将尝试在Wpf代码中的PasswordBox动态绑定隐藏动态为什么但不起作用 passwordBox.SetBinding(PasswordBox.DataContextProperty, new Binding(strValueBinding));

我正在使用

textboxValue.SetBinding(TextBox.TextProperty new Binding(strValueBinding));
此语法用于文本框动态绑定其工作状态。。 类似地,我将尝试在Wpf代码中的PasswordBox动态绑定隐藏动态为什么但不起作用

passwordBox.SetBinding(PasswordBox.DataContextProperty, new Binding(strValueBinding));
                    passwordBox.SetBinding(PasswordBox.PasswordCharProperty, new Binding(strValueBinding));
我可以尝试两者,但绑定问题并没有解决
任何人都知道wpf C#

中的动态绑定密码箱,由于MSDN的安全限制,您无法为密码箱设置绑定

可以通过MVVM实现密码箱绑定

希望这有帮助


Jsharma

我可以使用这个类并将其绑定到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#进行动态绑定。。。在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 });

它的工作方式……。

就是您要寻找的。@dotNET我正在寻找密码箱Wpf后面的代码的动态绑定。。。。。。。。。。。。。。。。。。。。。。在c的帮助下#