WPF MVVM 文本框文本绑定与 changedText 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20089739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF MVVM textbox text binding vs changedText event
提问by user1736332
I have textbox with Text property binded to viewmodel. I have binded TextChanged event to viewmodel.
我有 Text 属性绑定到 viewmodel 的文本框。我已将 TextChanged 事件绑定到视图模型。
But the problem is that the event fires every sign is added to textbox(that's good) and Text in viewmodel is refreshed when textbox lost focus(that's bad for me).
但问题是,当文本框失去焦点(这对我不利)时,事件会触发每个添加到文本框的符号(这很好)并且视图模型中的文本会刷新。
How to make this Text property refreshing after each sign?
如何在每次签名后刷新此 Text 属性?
Or how to send sender as parameter? I think sender would have resfreshed data.
或者如何将发件人作为参数发送?我认为发件人会刷新数据。
My interactivity xaml:
我的交互xaml:
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="TextChanged">
<helpers:CustomCommandInvoker Command="{Binding UnlockChangedCommand}" />
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
I'm using RelayCommand.
我正在使用 RelayCommand。
回答by Espen Medb?
There is a really simple answer to this; use UpdatePropertyTrigger=PropertyChanged. This will update your VM property each time a character is changed. No need for triggers or commands :-)
对此有一个非常简单的答案;使用 UpdatePropertyTrigger=PropertyChanged。每次更改字符时,这都会更新您的 VM 属性。不需要触发器或命令:-)
<TextBox Text="{Binding ViewModelProperty, UpdateSourceTrigger=PropertyChanged}"></TextBox>
Read here for more info: http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx
阅读此处了解更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx