MVVM 视觉状态管理器和焦点
使用 Silverlight 4。
我有两种视觉状态可供控制。我想在状态发生变化时将焦点从一个文本框更改为另一个文本框。
使用 MVVM 执行此操作的最佳方法是什么?
我希望使用 VisualStateManager 来做到这一点或一种行为......但我还没有找到办法。
Using Silverlight 4.
I have two visual states for my control. I want to change the focus from one textbox to another when the states change.
What is the best way to do this using MVVM?
I was hoping to use the visualstatemanager to do it or a behavior... but I have not figured out a way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我是您,我会创建一个具有 FocusBehavior.IsFocused 属性的 FocusBehaviour,在您的控件上添加该行为,并在 VSM 状态中设置 IsFocused=True。
If I were you I'd create a FocusBehaviour, with a FocusBehavior.IsFocused property, add that Behaviour on your Control and in the VSM state set IsFocused=True.
更改文本框之间的焦点绝对是特定于视图的代码,因此我认为它可能应该在视图后面的代码中完成。有些人建议根本没有代码,但我认为这有点夸张。
至于如何从 ViewModel 触发它,我会这样做:
Changing the focus between text boxes is most definitely view-specific code so I think it should probably be done in the code behind of the view. Some people suggest having no code at all but I think that's a bit of an exaggeration.
As for how to trigger it from the ViewModel, I would do something like:
来自 C#er 博客的解决方案< /a> 与 JustinAngle 的答案非常相似,但我认为因为它是 Silverlight 特定的解决方案,所以值得一提。基本上 Jeremy Likeness 创建了一个虚拟控件,他称之为 FocusHelper,其行为与 FocusBehavior 非常相似。
The solution from the C#er blog is pretty similar to JustinAngle's answer but I figured since it is a Silverlight specific solution it bears mentioning. Basically Jeremy Likeness creates a dummy control that he calls FocusHelper that behaves very much like FocusBehavior.