C# 如何在密码框中获取突出显示的文本
我有一个密码框,我正在添加一个“显示密码”按钮(如图所示): 密码文本框的相关XAML如下所示:C# 如何在密码框中获取突出显示的文本,c#,wpf,C#,Wpf,我有一个密码框,我正在添加一个“显示密码”按钮(如图所示): 密码文本框的相关XAML如下所示: <Border VerticalAlignment="Center" Grid.Row="2" Margin="2" Grid.Column="1" BorderThickness="1" BorderBrush="Black" Bac
<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>
我遇到的问题是,有一个“记住密码”选项,我不希望用户能够看到密码,除非密码文本框已“清除”所有文本至少一次(以防止用户看到所有或部分自动放入的密码)。上面的代码很接近,但是,如果用户选择所有文本,然后按键开始新密码,“空”密码不会通过密码更改事件出现,“显示密码”按钮将不可见。用户必须删除所有文本,然后重新开始键入。我想了解这样一种情况:用户选择所有文本,然后用户开始键入
如果我预览按键关闭事件,则密码框上没有“SelectedText”属性,因此我不知道如何提前知道是否会在添加新文本之前清除密码
有没有办法知道密码箱密码已被清除,和/或知道是否已选中所有文本?这是一个非常棘手的问题。他们不会公开披露这些信息,但你可以通过一些反思来获得这些信息。这有点令人讨厌,因为他们试图用内部类和私有属性隐藏所有内容,但你可以这样做:
TextSelection selection = (TextSelection)typeof(PasswordBox).GetProperty("Selection", BindingFlags.Instance | BindingFlags.NonPublic).GetMethod.Invoke(textBox, null);
Type tTextRange = selection.GetType().GetInterfaces().Where(x => x.Name == "ITextRange").FirstOrDefault();
object oStart = tTextRange.GetProperty("Start").GetMethod.Invoke(selection, null);
object oEnd = tTextRange.GetProperty("End").GetMethod.Invoke(selection, null);
MethodInfo mi = oStart.GetType().GetProperty("Offset", BindingFlags.Instance | BindingFlags.NonPublic).GetMethod;
int nStart = (int)mi.Invoke(oStart, null);
int nEnd = (int)mi.Invoke(oEnd, null);
System.Diagnostics.Debug.WriteLine(nStart + " ==> " + nEnd);
当然,您应该缓存一些反射内容,但我将留给您:)
注意:您需要在文本框_PreviewKeyDown()中获取开始和结束。当您更改密码时,所选内容显然已丢失。如果要检查是否选中了整个密码,则需要检查0==>len
TextSelection selection = (TextSelection)typeof(PasswordBox).GetProperty("Selection", BindingFlags.Instance | BindingFlags.NonPublic).GetMethod.Invoke(textBox, null);
Type tTextRange = selection.GetType().GetInterfaces().Where(x => x.Name == "ITextRange").FirstOrDefault();
object oStart = tTextRange.GetProperty("Start").GetMethod.Invoke(selection, null);
object oEnd = tTextRange.GetProperty("End").GetMethod.Invoke(selection, null);
MethodInfo mi = oStart.GetType().GetProperty("Offset", BindingFlags.Instance | BindingFlags.NonPublic).GetMethod;
int nStart = (int)mi.Invoke(oStart, null);
int nEnd = (int)mi.Invoke(oEnd, null);
System.Diagnostics.Debug.WriteLine(nStart + " ==> " + nEnd);