WPF-MVVM:从 ViewModel 设置 UI 控制焦点 [英] WPF-MVVM: Setting UI control focus from ViewModel

查看:30
本文介绍了WPF-MVVM:从 ViewModel 设置 UI 控制焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MVVM 架构中设置控制焦点的好做法是什么.

What is a good practice of setting control focus in MVVM architecture.

我设想的方式是在 ViewModel 上使用一个属性,该属性会在需要时触发焦点更改.然后让 UI 控件绑定/侦听该属性,以便在它发生更改时设置适当的焦点.

The way I envision it, is with a property on the ViewModel that would trigger a focus change when needed. And than have the UI controls bind/listen to that property so that if it changes, appropriate focus will be set.

我将其视为 ViewModel 的事情,因为我想在 ViewModel 执行特定操作(例如加载特定数据)后适当设置焦点.

I see it as a ViewModel thing, because i want to set focus appropriate after a certain action was performed by the ViewModel, such as loading certain data.

最佳实践是什么?

推荐答案

按照此处的答案中的建议使用 IsFocused 附加属性:从视图模型 (C#) 中将焦点设置在 WPF 中的文本框

Use the IsFocused Attached Property as suggested in the Answer here: Set focus on textbox in WPF from view model (C#)

然后你可以简单地绑定到你的视图模型中的一个属性.

Then you can simply bind to a property in your viewmodel.

这篇关于WPF-MVVM:从 ViewModel 设置 UI 控制焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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