WPF密码盒丢失了密码中的零字符



嗨,有人能帮我一下吗?我的任务是修复WPF登录过程中的错误(我没有以前的经验!)问题似乎是PasswordBox控件忽略了输入密码中的零。我已经检查了密码何时更改并检查了输入的值-它绝对忽略了'0'字符。例如密码应该是'password012',但是从Passwordbox返回的是'password12'类似'0password'返回'password'。用户名输入(TextBox控件)似乎没有问题。

谁能告诉我如何克服这个问题?xaml代码在这里
<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);
        }
    }
}

我在视图模型中发现了一些代码(感谢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;
    }

你在球场的正确部分。它是视图模型中的键盘钩子例程。它忽略48以下的键码。我现在修改了它(并添加了评论),改为忽略47以下。ASCII 48是字符'0'。非常感谢你帮我到那里。在您打开灯之前,我必须浏览这段代码几次。

相关内容

  • 没有找到相关文章

最新更新