wpf 密码框绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/888466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:27:26  来源:igfitidea点击:

PasswordBox Binding

wpfmvvmbindingpasswordbox

提问by djschwartz

I'm just getting started with M-V-VM and WPF and having issues understanding some binding issues.

我刚刚开始使用 MV-VM 和 WPF,并且在理解某些绑定问题时遇到了问题。

I have a login page that has a ComboBoxand a PasswordBox. The ComboBoxlooks like this:

我有一个登录页面,其中有一个ComboBoxPasswordBox。该ComboBox如下所示:

<ComboBox Name="comboBox1" SelectedItem="{Binding Path=Username}">

This works just fine - my values get updated everytime the SelectedItemchanges on the ComboBox!

这工作得很好 - 每次SelectedItem更改时我的值都会更新ComboBox

In my ViewModel I have an ICommandwhich uses this method to determine if the Login button is active:

在我的 ViewModel 中,我有一个ICommand使用此方法来确定登录按钮是否处于活动状态的:

public bool CanLogin()
{
    return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
}

So my problem is I don't have the PasswordBoxbound to the Password property on the ViewModel - so I have no way to tell when it is updated.

所以我的问题是我没有PasswordBox绑定到 ViewModel 上的 Password 属性 - 所以我无法知道它何时更新。

So how do I get the value of the PasswordBoxto my ViewModel? Everything I've read just says don't bind a PasswordBoxfor security reasons. I would simply take off the password restriction on the CanLogin() but I need the value to pass along to an AccountService.

那么如何获取PasswordBoxViewModel的值呢?我读过的所有内容只是说PasswordBox出于安全原因不要绑定 a 。我只想取消对 CanLogin() 的密码限制,但我需要将值传递给 AccountService。

回答by ArielBH

Interesting.

有趣的。

look at this blog post and see if it is helping you. http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html

看看这篇博文,看看它是否对你有帮助。 http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html

Apparently the link is dead now so here is the original solution (found here):

显然链接现在已经失效,所以这里是原始解决方案(在这里找到):

You can use attached properties to create a helper like this:

您可以使用附加属性来创建这样的助手:

public static class PasswordHelper
{
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password",
        typeof(string), typeof(PasswordHelper),
        new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

    public static readonly DependencyProperty AttachProperty =
        DependencyProperty.RegisterAttached("Attach",
        typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));

    private static readonly DependencyProperty IsUpdatingProperty =
       DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), 
       typeof(PasswordHelper));


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

Use it:

用它:

<PasswordBox w:PasswordHelper.Attach="True" 
             w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}" 
             Width="100"/>

回答by Taylor Leese

I posted a GIST herethat is a bindable password box.

我在这里发布了一个 GIST 它是一个可绑定的密码框。