Windows Store TextBox - How to update binding on Enter key-up?

笑着哭i 提交于 2020-01-06 10:51:40

问题


In a Windows Store app I have a TextBox and I've created an attached property KeyUpEvent.MapsToCommand that looks like this in use:

<TextBox 
    Text="{Binding UserGivenName, Mode=TwoWay}" 
    attachedProps:RequiresText.ToEnableControl="{Binding ElementName=SubmitNameButton}"
    attachedProps:KeyUpEvent.MapsToCommand="{Binding EnterNameCommand}"
/>

The problem is that the control doesn't lose focus so the binding is never updated. There's no UpdateSourceTrigger and I can't work out a way to get at the binding itself and manually update it in code, in the attached property.

Am I doing this wrong? How are people writing apps today if its not possible to hit Enter on the keyboard and invoke a command, at least with correct view model data anyway?

I've hit these seemingly small but enormous issues pretty much continually in XAML since 2008 and I'm getting rather fed up with it.

Thanks

Luke

Update

Found that this is a well-known pain point.

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/775f1692-2837-471c-95fc-710bf0e9cc53/updatesourcetrigger-and-bindingexpressionbase-are-missing?forum=winappswithcsharp

I am now months into trying to build an MVVM Windows Store app and I'm considering giving up.


回答1:


Just some ideas:

  • You could abuse a SearchBox which should update the Binding on Enter key. If not, you can programatically unfocus the control in the QuerySubmitted event handler (which also could invoke the command). For a plain TextBox you could handle its TextChanged event.
  • Inherit from TextBox and override OnKeyUp method.
  • See http://weblogs.asp.net/broux/archive/2012/07/03/windows-8-update-textbox-s-binding-on-textchanged.aspx
  • Use Windows 8.1


来源:https://stackoverflow.com/questions/23364138/windows-store-textbox-how-to-update-binding-on-enter-key-up

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!