喜计算器的人。我与MVVM工作,我的视图模型拨打UserViewModel与物业密码。在视图具有控制PasswordBox。

<PasswordBox x:Name="txtPassword" Password="{Binding Password}" />

但这个XAML不工作。你是怎么做到的?结合请帮助!!

有帮助吗?

解决方案

出于安全原因,密码属性不是依赖属性,因此,你不能结合到它。不幸的是你需要执行的老式方法后面的代码绑定(注册OnPropertyChanged事件,并通过代码更新值...)


我快速搜索使我这篇博客它展示了如何写附加属性来回避这个问题。无论这是值得做的事情与否虽然实际上取决于你所厌恶的代码隐藏。

其他提示

您总是可以编写一个封装了密码,并增加了一个依赖属性的密码属性的控制。

我只想用后面的代码,但如果你必须你可以这样做:

public class BindablePasswordBox : Decorator
{
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox));

    public string Password
    {
        get { return (string)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    public BindablePasswordBox()
    {
        Child = new PasswordBox();
        ((PasswordBox)Child).PasswordChanged += BindablePasswordBox_PasswordChanged;
    }

    void BindablePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        Password = ((PasswordBox)Child).Password;
    }

}

有与BindablePasswordBox的问题。它只能在一个方向,PasswordBox到PasswordProperty。下面是在两个方向上工作它的修改版本。它会注册一个PropertyChangedCallback,当它被称为更新PasswordBox的密码。 我希望有人认为这是有用的。

public class BindablePasswordBox : Decorator
{
    public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox), new PropertyMetadata(string.Empty, OnDependencyPropertyChanged));
    public string Password
    {
        get { return (string)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    private static void OnDependencyPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        BindablePasswordBox p = source as BindablePasswordBox;
        if (p != null)
        {
            if (e.Property == PasswordProperty)
            {
                var pb = p.Child as PasswordBox;
                if (pb != null)
                {
                    if (pb.Password != p.Password)
                        pb.Password = p.Password;
                }
            }
        }
    }

    public BindablePasswordBox()
    {
        Child = new PasswordBox();
        ((PasswordBox)Child).PasswordChanged += BindablePasswordBox_PasswordChanged;
    }

    void BindablePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        Password = ((PasswordBox)Child).Password;
    }
}

要避免在任何点具有在存储器中作为纯文本可用的密码,我提供该值作为参数传递给我的命令。

<Label>User Name</Label>
<TextBox Text="{Binding UserName}" />
<Label>Password</Label>
<PasswordBox Name="PasswordBox" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0 16 0 0">
    <Button Margin="0 0 8 0" MinWidth="65" 
            Command="{Binding LoginAccept}" 
            CommandParameter="{Binding ElementName=PasswordBox}">
        Login
    </Button>
    <Button MinWidth="65" Command="{Binding LoginCancel}">Cancel</Button>
</StackPanel>

然后在我的视图模型

public DelegateCommand<object> LoginAccept { get; private set; }
public DelegateCommand<object> LoginCancel { get; private set; }

public LoginViewModel {
    LoginAccept = new DelegateCommand<object>(o => OnLogin(o), (o) => IsLoginVisible);
    LoginCancel = new DelegateCommand<object>(o => OnLoginCancel(), (o) => IsLoginVisible);
}

private void OnLogin(object o)
{
    var passwordBox = (o as System.Windows.Controls.PasswordBox);
    var password = passwordBox.SecurePassword.Copy();
    passwordBox.Clear();
    ShowLogin = false;
    var credential = new System.Net.NetworkCredential(UserName, password);
}

private void OnLoginCancel()
{
    ShowLogin = false;
}

虽然这将是有意义的直接从结合提供SecurePassword,它似乎总是提供一个空值。因此,这不工作:

    <Button Margin="0 0 8 0" MinWidth="65" 
            Command="{Binding LoginAccept}" 
            CommandParameter="{Binding ElementName=PasswordBox, Path=SecurePassword}">

检查密码框另一个线程。 它的最好不要让密码在任何DP或公共财产。

上passwordbox

其他线程

scroll top