【发布时间】:2014-09-19 13:26:36
【问题描述】:
嗨,有人可以帮我吗? 我的任务是修复 WPF 中登录过程中的错误(我以前没有经验!) 问题似乎是 PasswordBox 控件忽略了输入密码中的零。我检查了密码何时更改并检查了输入的值 - 它肯定会忽略“0”字符。示例密码应该是“password012”,但从 Passwordbox 返回的是“password12”,类似于“0password”返回“password”。 用户名条目(TextBox 控件)看起来不错。
有人可以建议如何克服这个问题吗? xml代码在这里
<TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,48" Foreground="DarkSlateGray" Grid.Column="1" Grid.Row="1" Text="{Binding Username}" Name="txtUsername" Height="26" HorizontalContentAlignment="Center" Style="{StaticResource RoundTextBoxStyle}" Width="200" IsUndoEnabled="False" Effect="{StaticResource TextBoxDropShadow}" FontFamily="{StaticResource TextBoxDescrptionFontFamily}" FontSize="{StaticResource TextBoxDescriptionFontSize}" GotFocus="TxtBoxGotFocus" ToolTip="Enter your Username." Background="{StaticResource TextBoxBackground}"/>
<PasswordBox Grid.Column="1" Foreground="#FF2F4F4F" Margin="0,0,0,-36" VerticalAlignment="Center" Grid.Row="1" HorizontalAlignment="Center" Height="26" HorizontalContentAlignment="Center" MaxLength="0" w:PasswordBoxBinder.Attach="True" Style="{StaticResource RoundTextBoxStyle}" w:PasswordBoxBinder.Password="{Binding Path=Password, Mode=TwoWay}" Effect="{StaticResource TextBoxDropShadow}" Width="{Binding ElementName=txtUsername, Path=Width}" ToolTip="Enter your Password." GotFocus="PbxGotFocus" FontFamily="{StaticResource TextBoxDescrptionFontFamily}" FontSize="{StaticResource TextBoxDescriptionFontSize}" Background="{StaticResource TextBoxBackground}" PasswordChanged="PasswordBox_PasswordChanged" />
PasswordBox_PasswordChanged 的代码是我自己添加的,用于调试以了解发生了什么。
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
// ... Display Password in Title.
// A bad design decision.
var box = sender as PasswordBox;
this.Title = "Password typed: " + box.Password;
}
PasswordBoxBinder 的代码在这里,是原始代码的一部分: 公共静态类 PasswordBoxBinder {
//test
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordBoxBinder),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordBoxBinder), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordBoxBinder));
public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
var passwordBox = sender as PasswordBox;
if (passwordBox != null)
{
passwordBox.PasswordChanged -= PasswordChanged;
if (!GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void Attach(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
var passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
var passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
if (passwordBox != null)
{
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}
}
我在 View 模型中找到的一些代码(非常感谢 Den)。
private static bool KhKeyIntercepted(KeyBoardHook.KeyboardHookEventArgs e)
{
if (e.KeyCode < 65 || e.KeyCode > 90) //a - z
{
if (e.KeyCode > 47 && e.KeyCode < 57) // 0-9
return true;
if (e.KeyCode == 220
|| e.KeyCode == 191
|| e.KeyCode == 189
|| e.KeyCode == 160
|| e.KeyCode == 161
|| e.KeyCode == 8
|| e.KeyCode == 9) //slsah, minus, left and right shift, tab and backspace
return true;
return false;
}
return true;
}
【问题讨论】:
-
请公开PasswordBoxBinder和PasswordBox_PasswordChanged handler的代码
-
嗨。我自己添加了 PasswordBox_PasswordChanged 处理程序来检查返回的字符,因此这可能没有用,但它是:
-
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) { // ... 在标题中显示密码。 // 一个糟糕的设计决策。 var box = 作为 PasswordBox 的发件人; this.Title = "输入的密码:" + box.Password; }
-
PasswordboxBinder 似乎也是这里的助手 Password.helper 的一部分。非常感谢您在这方面的帮助。代码似乎太长了,所以我不确定如何在此处附加(新手!!)
-
编辑您的问题以添加所需的代码