文本更改时的密码框验证而不是失去焦点

发布于 2024-12-29 02:30:22 字数 932 浏览 2 评论 0原文

框架:Silverlight 4

我有一个简单的ChildWindow,用户名TextBox 和密码PasswordBox。我已将事件处理程序附加到窗口的 KeyDown 事件。

private void onKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Escape)
        cancelButtonClick(null, null);

    if (e.Key == Key.Enter)
        okButtonClick(null, null);
}

我想要实现的是,当用户按下键盘上的 Enter 键时,程序的行为就像用户单击了“确定”按钮一样。

问题是验证。

Silverlight 的PasswordBox 的默认行为是在控件失去焦点时执行验证。我的 PasswordBox 绑定到某个 User 对象。当我单击 Enter 按钮时,将调用事件处理程序,该处理程序又调用 okButtonClick(null, null)。问题在于,当时 PasswordBox 尚未失去焦点,因此 PasswordBox 所绑定的 user.Password 属性,仍然是空的。

我尝试将 btnOK.Focus() 放在 okButtonClick(null, null) 之前,但无济于事。

如何设置绑定,以便控件在每次文本更改时更新绑定,而不是在 LostFocus 事件时更新绑定?实现我需要的正确方法是什么?

Framework: Silverlight 4

I have a simple ChildWindow with Username TextBox and Password PasswordBox. I have attached an event handler to the window's KeyDown event.

private void onKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Escape)
        cancelButtonClick(null, null);

    if (e.Key == Key.Enter)
        okButtonClick(null, null);
}

What I'm trying to achieve is that when the user presses Enter key on the keyboard, the program will behave as if the user had clicked the OK button.

The problem is the validation.

The default behavior for Silverlight's PasswordBox is to perform validation when the control loses focus. My PasswordBox is bound to some User object. When I click the Enter button, the event handler gets called, which in turn calls the okButtonClick(null, null). The problem is that the PasswordBox has not yet lost the focus at that time, so the user.Password property, which the PasswordBox is bound to, is still empty.

I've tried to place btnOK.Focus() before the okButtonClick(null, null) but to no avail.

How to set the binding so that the control will update binding on every text change instead of on the LostFocus event? What is the right way to achieve what I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2025-01-05 02:30:22

Passwordbox 使用 PasswordChanged 事件。
TextboxTextboxChanged 事件。

Use PasswordChanged event for Passwordbox.
Textbox has TextboxChanged event.

潇烟暮雨 2025-01-05 02:30:22

我通过绊倒 根据此解决方案

它基本上使用附加属性来订阅 TextChanged/PasswordChnaged 事件,然后在该事件中更新绑定源。调整后 满足我的需求的解决方案,这就是我得到的(并且它工作完美):

public class BindingHelper
{
    public static readonly DependencyProperty RefreshOnChangeProperty =
        DependencyProperty.RegisterAttached("RefreshOnChange", typeof(bool), typeof(BindingHelper),
        new PropertyMetadata(false, OnRefreshOnChangeChanged));

    public static void SetRefreshOnChange(DependencyObject o, bool value)
    {
        o.SetValue(RefreshOnChangeProperty, value);
    }

    public static bool GetRefreshOnChange(DependencyObject o)
    {
        return (bool)o.GetValue(RefreshOnChangeProperty);
    }

    private static void OnRefreshOnChangeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if ((obj as TextBox) != null)
        {
            if ((bool)e.NewValue)
                (obj as TextBox).TextChanged += textBox_TextChanged;
            else
                (obj as TextBox).TextChanged -= textBox_TextChanged;
        }

        if ((obj as PasswordBox) != null)
        {
            if ((bool)e.NewValue)
                (obj as PasswordBox).PasswordChanged += passwordBox_PasswordChanged;
            else
                (obj as PasswordBox).PasswordChanged -= passwordBox_PasswordChanged;
        }
    }

    static void passwordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        PasswordBox passwordBox = sender as PasswordBox;
        if (passwordBox != null)
        {
            BindingExpression binding = passwordBox.GetBindingExpression(PasswordBox.PasswordProperty);
            if (binding != null)
                binding.UpdateSource();
        }
    }

    static void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            BindingExpression binding = textBox.GetBindingExpression(TextBox.TextProperty);
            if (binding != null)
                binding.UpdateSource();
        }
    }
}

I have solved the problem by stumbling upon this solution.

It basicly uses attached property to subscribe to the TextChanged/PasswordChnaged events, and then in the event it updates the binding source. After adapting the solution to my needs, here is what I got (and it works flawlessly):

public class BindingHelper
{
    public static readonly DependencyProperty RefreshOnChangeProperty =
        DependencyProperty.RegisterAttached("RefreshOnChange", typeof(bool), typeof(BindingHelper),
        new PropertyMetadata(false, OnRefreshOnChangeChanged));

    public static void SetRefreshOnChange(DependencyObject o, bool value)
    {
        o.SetValue(RefreshOnChangeProperty, value);
    }

    public static bool GetRefreshOnChange(DependencyObject o)
    {
        return (bool)o.GetValue(RefreshOnChangeProperty);
    }

    private static void OnRefreshOnChangeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if ((obj as TextBox) != null)
        {
            if ((bool)e.NewValue)
                (obj as TextBox).TextChanged += textBox_TextChanged;
            else
                (obj as TextBox).TextChanged -= textBox_TextChanged;
        }

        if ((obj as PasswordBox) != null)
        {
            if ((bool)e.NewValue)
                (obj as PasswordBox).PasswordChanged += passwordBox_PasswordChanged;
            else
                (obj as PasswordBox).PasswordChanged -= passwordBox_PasswordChanged;
        }
    }

    static void passwordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        PasswordBox passwordBox = sender as PasswordBox;
        if (passwordBox != null)
        {
            BindingExpression binding = passwordBox.GetBindingExpression(PasswordBox.PasswordProperty);
            if (binding != null)
                binding.UpdateSource();
        }
    }

    static void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            BindingExpression binding = textBox.GetBindingExpression(TextBox.TextProperty);
            if (binding != null)
                binding.UpdateSource();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文