首页 文章

WPF PasswordBox从密码中丢失零字符

提问于
浏览
0

嗨,有人可以帮帮我吗?我的任务是修复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的代码在这里,是原始代码的一部分:public static class 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;
    }

1 回答

  • 0

    你在球场的正确部分 . 它是View Model中的键盘钩子例程 . 它反映了48以下的关键代码 . 我现在修改了它(并添加了评论)而不是低于47 . ASCII 48是字符'0' . 非常感谢你帮助我到那里 . 我必须多次浏览这段代码,直到你打开灯 .

相关问题