我有一个密码框,我正在添加一个“显示密码”按钮(如图所示):
密码文本框的相关XAML如下所示:
<Border VerticalAlignment="Center" Grid.Row="2" Margin="2" Grid.Column="1"
BorderThickness="1"
BorderBrush="Black"
Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<PasswordBox Name="txtPassword" Grid.Column="0" MaxLength="100" Margin="2" BorderThickness="0" GotFocus="txtPassword_GotFocus" PasswordChanged="txtPassword_PasswordChanged" />
<TextBox Name="txtVisiblePassword" Grid.Column="0" MaxLength="100" Margin="2" BorderThickness="0" Visibility="Collapsed" Height="22" />
<Button Grid.Column="1" Style="{StaticResource LightHoverButton}" x:Name="btnShowPassword" PreviewMouseDown="btnShowPassword_PreviewMouseDown" PreviewMouseUp="btnShowPassword_PreviewMouseUp">
<Image Source="{StaticResource ShowPassword_128}" Style="{StaticResource Image_16}" Margin="3,0" />
</Button>
</Grid>
</Border>
背后的相关代码:
private void txtPassword_PasswordChanged(object sender, RoutedEventArgs e)
{
//Can show password is set to false if the password being displayed has been
//placed there automatically because the password is being remembered
if (canshowpassword == false && string.IsNullOrEmpty(txtPassword.Password))
canshowpassword = true;
if (canshowpassword)
btnShowPassword.Visibility = string.IsNullOrEmpty(txtPassword.Password) ? Visibility.Collapsed : Visibility.Visible;
else
btnShowPassword.Visibility = Visibility.Collapsed;
}
我遇到的问题是,有一个“记住密码”选项,我不希望用户能够看到密码,除非密码文本框已被“清除”所有文本至少一次(以防止用户看到自动放在那里的全部或部分密码。上面的代码很接近,但是,它有一个问题,如果用户选择所有文本,然后按一个键开始一个新密码,“空”密码不会通过密码更改事件和“显示密码”按钮不会变得可见。用户必须删除所有文本,然后再次开始输入。我想了解用户选择所有文本并开始输入的情况。
如果我预览Key down事件,PasswordBox上没有“SelectedText”属性,所以我不知道如何在添加新文本之前清除密码是否会被清除。
有没有办法知道PasswordBox密码已被清除,和/或知道是否所有文本都被选中了?
答案 0 :(得分:2)
这个非常棘手。他们没有公开披露这些信息,但你可以反思一下。这有点令人讨厌,因为他们试图用内部类和私有属性隐藏所有东西,但是你走了:
#The main function definition
def main():
Name = input("What is your name? ")
Account_ID = input("What is your account ID? ")
Transaction_Code = input("Press W or w for Withdrawal, Press D or d for Deposit: ")
Previous_Balance = float(input("What is your previous balance? "))
Transaction_Amount = float(input("How much is the transaction amount? "))
if Transaction_Code == "W" or Transaction_Code == "w":
Withdrawal_Process(Previous_Balance, Transaction_Amount)
elif Transaction_Code == "D" or Transaction_Code == "d":
Deposit_Process(Previous_Balance, Transaction_Amount)
else:
Process_Invalid_Code(Previous_Balance)
#Defines the Deposit Process
def Deposit_Process(Previous_Balance, Transaction_Amount):
New_Balance = Transaction_Amount + Previous_Balance
Print_Function(New_Balance)
#Defines the Withdrawal Process
def Withdrawal_Process(Previous_Balance, Transaction_Amount):
if Transaction_Amount > Previous_Balance:
print("Invalid Transaction: Not Sufficient Funds!")
New_Balance = Previous_Balance
Print_Function(New_Balance)
else:
New_Balance = Previous_Balance - Transaction_Amount
Print_Function(New_Balance)
#The Invalid Code Function Definition
def Process_Invalid_Code(Previous_Balance):
New_Balance = Previous_Balance
print ("Invalid Transaction Code!")
print ("Please type W or w for Withdrawal")
print ("or type D or d for Deposit")
Print_Function(New_Balance)
#Defines the Print Function
def Print_Function(New_Balance):
print ("Your balance is now $", format(New_Balance, '.2f'))
#Call the main function
main()
当然,你应该缓存一些反思内容,但我会把它留给你:)。
注意:您需要在textBox_PreviewKeyDown()中开始和结束。到达PasswordChanged时,选择已经明显丢失。如果要检查是否选中了整个密码,则需要检查0 ==&gt; LEN。