如何让 Silverlight 数据绑定在用户键入时更新模型?

发布于 2024-08-21 13:02:02 字数 363 浏览 15 评论 0原文

我目前正在使用 Silverlight 4 并遵循 MVVM 模式。我有一个绑定到我的 ViewModel 的登录框,如下所示:

<PasswordBox Password="{Binding Path=Password, Mode=TwoWay}" />

然后我有一个按钮绑定到一个命令,该命令侦听 ViewModel 的 PropertyChanged 事件,并且当其中一个数据绑定更新其数据时,它会执行检查现在是否有足够的数据来启用登录按钮。

但是,PropertyChanged 事件仅在用户从其中一个控件更改焦点时触发,我希望每次击键时都会更新模型,以便登录按钮尽快启用。

I'm currently using Silverlight 4 and following the MVVM pattern. I have login boxes bound to my ViewModel like so:

<PasswordBox Password="{Binding Path=Password, Mode=TwoWay}" />

I then later on have a button bound to a Command which listens to the ViewModel's PropertyChanged event and when one of the databindings has its data updated, it does a check to see if there is now enough data to enable the Login button.

However, the PropertyChanged event only fires when the user changes focus from one of the controls, I'd like the model to be updated with every keystroke so that the login button enables as soon as possible.

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

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

发布评论

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

评论(2

白日梦 2024-08-28 13:02:02

创建一个行为:

public class UpdateSourceOnPasswordChanged : Behavior<PasswordBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.PasswordChanged += OnPasswordChanged;
    }

    private void OnPasswordChanged(object sender, RoutedEventArgs e)
    {
        var binding = AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty);
        binding.UpdateSource();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.PasswordChanged -= OnPasswordChanged;
    }
}

并修改您的 xaml:

<PasswordBox Password="{Binding Password, Mode=TwoWay}">
    <i:Interaction.Behaviors>
        <local:UpdateSourceOnPasswordChanged/>
    </i:Interaction.Behaviors>
</PasswordBox>

现在,属性“密码”将随着用户类型而更新。

Create a behavior:

public class UpdateSourceOnPasswordChanged : Behavior<PasswordBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.PasswordChanged += OnPasswordChanged;
    }

    private void OnPasswordChanged(object sender, RoutedEventArgs e)
    {
        var binding = AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty);
        binding.UpdateSource();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.PasswordChanged -= OnPasswordChanged;
    }
}

And modify your xaml:

<PasswordBox Password="{Binding Password, Mode=TwoWay}">
    <i:Interaction.Behaviors>
        <local:UpdateSourceOnPasswordChanged/>
    </i:Interaction.Behaviors>
</PasswordBox>

Now the property Password will update as user types.

帅气尐潴 2024-08-28 13:02:02

我建议使用一种行为来侦听 PasswordBox 的 OnKeyDown 事件并从那里触发 ViewModel 的事件(或运行您想要附加到 PropertyChanged 事件的其他一些自定义代码)。 TextBox 及其派生项(如 PasswordBox)的数据绑定在失去焦点之前不会更新,因此您必须手动更新绑定。

I'd recommend using a behavior that listens for the PasswordBox's OnKeyDown event and fires your ViewModel's event from there (or runs some other piece of custom code that you wanted to attach to the PropertyChanged event). Databinding for TextBoxes and their derivitives (like PasswordBox) don't update until they lose focus, so you have to manually update the binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文