【发布时间】:2017-02-20 13:54:35
【问题描述】:
我在我的 Windows Phone 8.1 应用程序中为用户输入使用 TextBox 控件。
如何在用户输入时隐藏字符?
我没有使用PasswordBox,因为定义的InputScope 是"Number",这在PasswordBox 中是不可能的。
在互联网上搜索解决方案时,我发现唯一的方法是在 UserControl 的帮助下自定义 TextBox。
有没有更简单的方法可以在不创建任何UserControl 的情况下做到这一点?
以下是我的代码 sn-p:
在 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 事件并用星号替换字母吗?
-
我在 TextBox 控件上没有找到 PreviewKeyDown 事件。而是有 KeyDown 事件。我是这个领域的新手。我正在尝试任何可能的解决方案,但仍然没有结果。
标签: wpf xaml textbox windows-phone-8.1 passwordbox