我怎么能告诉我的视图模型,用户在文本框更改文字? [英] How can I tell my ViewModel that the user is changing text in the TextBox?

查看:116
本文介绍了我怎么能告诉我的视图模型,用户在文本框更改文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们可以说我有一个 MVVM 应用程序,我希望用户填写的文本框,而他正在填补其退出我要检查,看看他在姓客户的已键入呢。

下面是我如何得到我的ViewModel知道什么时候该用户的在ComboBox 更改的项目

 <组合框
    的ItemsSource ={结合客户}
    的ItemTemplate ={StaticResource的CustomerComboBoxTemplate}
    保证金=20
    的Horizo​​ntalAlignment =左
    的SelectedItem ={结合SelectedCustomer,模式=双向}/>

这是我如何得到我的ViewModel知道什么时候该用户的移动滑块

 <滑块最小值=0
        保证金=10
        宽度=400
        IsSnapToTickEnabled =真
        最大={结合HighestCustomerIndex,模式=双向}
        VALUE ={结合SelectedCustomerIndex,模式=双向}/>

这是我如何得到我的ViewModel知道什么时候该用户的在文本框中改变文本和感动的焦点远从文本框:

 <文本框
    WIDTH =200
    文本={结合TypedCustomerName}/>

但我怎么得到我的ViewModel知道什么时候该用户的在TextBox,因为他的类型,例如改变文本是这样的:

伪code(会导致错误,因为框TextChanged是一个事件):

 <文本框
    WIDTH =200
    框TextChanged ={结合CurrentTextInTextBox}/>


解决方案

如果你喜欢,而不是只在更新视图模型时,文本框失去焦点,您可以设置它来更新他们的类型。在<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx\">UpdateSourceTrigger在一个TextBox的Text属性绑定在默认情况下,而不是PropertyChanged的像大多数其他控件设置为引发LostFocus,但是你可以明确地在绑定设置。通过这样做,因为它在用户界面被改变在VM或M TypedCustomerName属性将更新。

 &LT;文本框
WIDTH =200
文本={结合TypedCustomerName,UpdateSourceTrigger =的PropertyChanged}/&GT;

如果这不是你要找的内容,你也可以使用<一个href=\"http://marlongrech.word$p$pss.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/\">AttachedCommandBehaviors到框TextChanged路由事件绑定到存在于您的视图模型一个ICommand。

So let's say I have an MVVM application and I want the user to fill out a TextBox and while he is filling it out, I want to check to see if he has typed in the last name of a customer yet.

Here is how I get my ViewModel to know when the user has changed the item in the ComboBox:

<ComboBox 
    ItemsSource="{Binding Customers}"
    ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
    Margin="20"
    HorizontalAlignment="Left"
    SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>

And here is how I get my ViewModel to know when the user has moved the Slider:

<Slider Minimum="0" 
        Margin="10"
        Width="400"
        IsSnapToTickEnabled="True"
        Maximum="{Binding HighestCustomerIndex, Mode=TwoWay}" 
        Value="{Binding SelectedCustomerIndex, Mode=TwoWay}"/>

And here is how I get my ViewModel to know when the user has changed text in the TextBox and moved the focus away from the TextBox:

<TextBox
    Width="200"
    Text="{Binding TypedCustomerName}"/>

But how do I get my ViewModel to know when the user has changed text in the TextBox as he types, e.g. something like this:

PSEUDO-CODE (causes error since TextChanged is an event):

<TextBox
    Width="200"
    TextChanged="{Binding CurrentTextInTextBox}"/>

解决方案

If you like, instead of only updating the ViewModel when the TextBox has lost focus, you can set it to update as they type. The UpdateSourceTrigger on the Text binding property of a TextBox is set to LostFocus by default instead of PropertyChanged like most other controls, however you can set it explicitly in the binding. By doing so, the TypedCustomerName property in the VM or M will update as it is changed in the UI.

<TextBox
Width="200"
Text="{Binding TypedCustomerName, UpdateSourceTrigger=PropertyChanged}"/>

If that's not what you're looking for, you could also use AttachedCommandBehaviors to bind the TextChanged routed event to an ICommand that exists in your View Model.

这篇关于我怎么能告诉我的视图模型,用户在文本框更改文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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