绑定 WPF 密码框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40981169/
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-13 13:57:54  来源:igfitidea点击:

Bind a WPF passwordbox

c#wpfxamlpasswordbox

提问by NoIdea123

I want to bind a Passwordbox in the XAML code. Now I found out that it isn′t possible to bind it because you haven′t any dependency property.

我想在 XAML 代码中绑定一个密码框。现在我发现不可能绑定它,因为你没有任何依赖属性。

For my case I don′t need any security in my Application. I just don′t want to display clear text to the user. Is there any other option to realize a kind of passwordbox with a textbox or so on?

就我而言,我的应用程序不需要任何安全性。我只是不想向用户显示明文。有没有其他选择来实现一种带有文本框等的密码框?

回答by Petter Hesselberg

The Passwordproperty is not a dependency property -- for security reasons. You can easily get the plain-text password, though.

Password出于安全原因,该属性不是依赖属性。不过,您可以轻松获得纯文本密码。

XAML:

XAML:

<PasswordBox
    x:Name="passwordBox"
    PasswordChanged="OnPasswordChanged" />

Code-behind event handler:

代码隐藏事件处理程序:

private void OnPasswordChanged(
    object sender,
    RoutedEventArgs e)
{
    Debug.WriteLine(passwordBox.Password);
}


Updates

更新