我在Windows Phone 8.1应用程序中使用用户输入的TextBox
控件。
如何隐藏用户输入的字符?在不使用密码框的情况下隐藏TextBox控件的文本
我没有使用PasswordBox
,因为定义的InputScope
是"Number"
,这在PasswordBox
中是不可能的。
在互联网上搜索解决方案时,我发现唯一的方法是在UserControl
的帮助下自定义TextBox
。
有没有更简单的方法来做到这一点,而无需创建任何UserControl
?
以下是我的代码片段:
在XAML页面:
<TextBox Text="{Binding CardNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MaxLength="17"
x:Name="CardNoTextBox"
InputScope="Number"
Margin="70,5"
PlaceholderText="Enter Your Card Number"
TextChanged="CardNoTextBox_TextChanged"
BorderBrush="Gray"
BorderThickness="2"
FontSize="20"/>
在后面的代码(xaml.cs):
private void CardNoTextBox_TextChanged(object sender, RoutedEventArgs routedEventArgs)
{
if (IsTextAllowed(CardNoTextBox.Text))
{
if (CardNoTextBox.Text.Length == 5)
{
if (CardNoTextBox.Text[4] != ' ')
{
string text = CardNoTextBox.Text.Insert(4, " ");
CardNoTextBox.Text = text;
CardNoTextBox.Select(CardNoTextBox.Text.Length, 0);
}
}
if (CardNoTextBox.Text.Length == 12)
{
if (CardNoTextBox.Text[11] != ' ')
{
string text = CardNoTextBox.Text.Insert(11, " ");
CardNoTextBox.Text = text;
CardNoTextBox.Select(CardNoTextBox.Text.Length, 0);
}
}
}
else
{
CardNoTextBox.Text = "";
}
}
你能捕捉到PreviewKeyDown事件并用星号替换该字母吗? – 0x4f3759df
我在TextBox控件上找不到任何PreviewKeyDown事件。有KeyDown事件。我是这个领域的新人。我正在尝试任何可能的解决方案,但仍然没有结果。 –