如何使ViewModel发生绑定 [英] How to make the binding happen from the ViewModel

查看:227
本文介绍了如何使ViewModel发生绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在捕获在文本框上按下的Enter键,以便我可以启动对服务器的更新。它不工作,所以我已经将问题减少到简单的元数据。

I'm trying to capture the Enter key being pressed on a text box, so that I can kick off an update to the server. It's not working, and so I've reduced the problem to it's simplist elemetns.

在这个例子中,似乎每次击键都不会发生绑定,但在某个时间之后。按下Enter键,我需要完成装订。考虑以下XAML和VM的功能。

In this example, it seems that the binding is not happening per keystroke, but at sometime later. I need to the binding to be completed by the time the enter key is pressed. Consider the following XAML and function from the VM.

这是文本框的XAML

Here's the XAML of the textbox

<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyDown">
            <cmd:EventToCommand Command="{Binding KeyDownCommand}"
                PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

KeyDownCommand如预期的那样火,值得还没有在TextValue属性中。如果我打了第二次,那么该值在属性中?这是KeyDownCommand。 ViewModel的构造函数正确设置keyDownCommand。

The KeyDownCommand fire as expected, howerver the value is not yet in the TextValue property. If I hit enter a second time then the value is in the property? Here's the KeyDownCommand. The constructor of the ViewModel sets the keyDownCommand correctly.

public RelayCommand<RoutedEventArgs> KeyDownCommand { get; private set; }
private void KeyDownAction(RoutedEventArgs eventArg)
{
    var source = eventArg.OriginalSource as FrameworkElement;
    var e = eventArg as KeyEventArgs;
    if (source != null && e != null && e.Key== Key.Enter)
    {
        e.Handled = true;
        MessageBox.Show(TextValue);
    }
}

似乎我需要的是一种当按下Enter键时,将TextBox的Text发送回VM的TextValue属性。或者还有一些我想要的东西。

It seems that what I need is a way to "post" the Text of the TextBox back to the TextValue property of the VM when the Enter key is pressed. Or is there something else I'm missing.

推荐答案

尝试设置 UpdateSourceTrigger PropertyChanged 绑定,如下所示:

Try setting UpdateSourceTrigger to PropertyChanged on binding, like this:

<TextBox Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" Width="300">

现在,每次更改文本时,视图模型属性将被更新。

Now the view model property will be update every time the text is changed.

更新:

对于Silverlight,替代 UpdateSourceTrigger ,您可以使用以下简单的行为,每当文本更改时更新绑定源:

For Silverlight, as an alternative to UpdateSourceTrigger, you can use the following simple behavior that updates binding source whenever text changes:

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

        AssociatedObject.TextChanged += OnTextChanged;
    }

    private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        var bindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);

        if (bindingExpression != null)
        {
            bindingExpression.UpdateSource();
        }
    }
}

使用如下: / p>

Use it like this:

<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
    <i:Interaction.Behaviors>
        <b:TextChangedUpdateSourceBehavior />
    </i:Interaction.Behaviors>
</TextBox>

这篇关于如何使ViewModel发生绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆