密码框绑定 [英] PasswordBox Binding

查看:18
本文介绍了密码框绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 M-V-VM 和 WPF,在理解一些绑定问题时遇到了问题.

我有一个登录页面,其中有一个 ComboBox 和一个 PasswordBox.ComboBox 看起来像这样:

这很好用 - 每次 SelectedItemComboBox 上发生变化时,我的值都会更新!

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

public bool CanLogin(){返回 !string.IsNullOrEmpty(用户名) &&!string.IsNullOrEmpty(密码);}

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

那么如何将 PasswordBox 的值获取到我的 ViewModel 中?我读过的一切只是说出于安全原因不要绑定 PasswordBox.我只想取消对 CanLogin() 的密码限制,但我需要将该值传递给 AccountService.

解决方案

有趣.

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

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

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

<前>公共静态类 PasswordHelper{public static readonly DependencyProperty PasswordProperty =DependencyProperty.RegisterAttached("密码",typeof(string), typeof(PasswordHelper),新 FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));public static readonly DependencyProperty AttachProperty =DependencyProperty.RegisterAttached("Attach",typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));私有静态只读 DependencyProperty IsUpdatingProperty =DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),typeof(PasswordHelper));公共静态无效SetAttach(DependencyObject dp,布尔值){dp.SetValue(AttachProperty, value);}public static bool GetAttach(DependencyObject dp){返回(布尔)dp.GetValue(AttachProperty);}公共静态字符串 GetPassword(DependencyObject dp){返回(字符串)dp.GetValue(密码属性);}public static void SetPassword(DependencyObject dp,字符串值){dp.SetValue(PasswordProperty, value);}私有静态布尔 GetIsUpdating(DependencyObject dp){返回(布尔)dp.GetValue(IsUpdatingProperty);}私有静态无效 SetIsUpdating(DependencyObject dp,布尔值){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;}私有静态无效附加(依赖对象发送者,DependencyPropertyChangedEventArgs e){PasswordBox passwordBox = sender as PasswordBox;如果(密码框 == 空)返回;if ((bool)e.OldValue){passwordBox.PasswordChanged -= PasswordChanged;}if ((bool)e.NewValue){passwordBox.PasswordChanged += PasswordChanged;}}私有静态无效密码更改(对象发送者,RoutedEventArgs e){PasswordBox passwordBox = sender as PasswordBox;SetIsUpdating(passwordBox, true);SetPassword(passwordBox, passwordBox.Password);SetIsUpdating(passwordBox, false);}}

使用它:

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

I have a login page that has a ComboBox and a PasswordBox. The ComboBox looks like this:

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

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

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

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

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

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

解决方案

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

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"/>

这篇关于密码框绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆