本文介绍了如何使TextBox成为“密码框"?和使用MVVM时显示星星?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在XAML中做到这一点?

How can I do this in XAML:

伪代码:

<TextBox Text="{Binding Password}" Type="Password"/>

,以便用户在输入密码时看到星号或点.

so that the user sees stars or dots when he is typing in the password.

我尝试了各种示例 PasswordChar PasswordBox ,但无法使它们正常工作.

I've tried various examples which suggest PasswordChar and PasswordBox but can't get these to work.

例如我可以按照此处所示的方式进行操作:

e.g. I can do this as shown here:

<PasswordBox Grid.Column="1" Grid.Row="1"
    PasswordChar="*"/>

但是我当然想将Text属性绑定到我的ViewModel上,以便在单击按钮时可以将绑定的TextBox值发送给我(不适用于后面的代码),我想这样做:

but I of course want to bind the Text property to my ViewModel so I can send the value the bound TextBox when the button is clicked (not working with code behind), I want to do this:

<TextBox Grid.Column="1" Grid.Row="0" 
    Text="{Binding Login}" 
    Style="{StaticResource FormTextBox}"/>
<PasswordBox Grid.Column="1" Grid.Row="1"
    Text="{Binding Password}" 
    PasswordChar="*"
    Style="{StaticResource FormTextBox}"/>

但是PasswordBox没有Text属性.

But PasswordBox doesn't have a Text property.

推荐答案

要在PasswordBox中获取或设置密码,请使用Password属性.如

To get or set the Password in a PasswordBox, use the Password property. Such as

string password = PasswordBox.Password;

据我所知,这不支持数据绑定,因此您必须在后面的代码中设置值,并进行相应的更新.

This doesn't support Databinding as far as I know, so you'd have to set the value in the codebehind, and update it accordingly.

这篇关于如何使TextBox成为“密码框"?和使用MVVM时显示星星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:22