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

问题描述

我刚开始使用M-V-VM和WPF和了解一些有约束力的问题,有问题。

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

我有,有一个组合框 PasswordBox 登录页面。在组合框是这样的:

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

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

这只是正常 - 我的价值得到每次更新上的组合框

在我的ViewModel我有一个的ICommand 使用这种方法来确定登录按钮被激活:

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

所以我的问题是我没有 PasswordBox 绑定到视图模型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.

那么,如何获得的 PasswordBox 来我的视图模型的价值?我读过的一切只是说,不绑定 PasswordBox 出于安全原因。我只想起飞的CanLogin(密码限制),但我需要的价值,一起到AccountService的通过。

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.

推荐答案

有趣的。

看这个博客帖子和看它是否帮助您。
http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html

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

使用它:

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

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

09-21 13:18