MVVM 焦点到文本框 [英] MVVM Focus To Textbox

查看:50
本文介绍了MVVM 焦点到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将焦点设置到 TextBox 而不指定 TextBox 的名称?目前我正在做以下事情

How would I set focus to a TextBox without specifying the name for that TextBox? At the moment I am doing the following

<Window FocusManager.FocusedElement="{Binding ElementName=Username}">
    <Grid>
        <TextBox Text="{Binding Username}" Name="Username" />            
    </Grid>
</Window>

是否有任何方法可以在不为 TextBox 指定名称的情况下执行此操作.我相信 MVVM 有一个 Name 元素通常意味着糟糕的设计?

Is there any way of doing this without specifying a Name for the TextBox. As I believe in MVVM having a Name element usually means bad design?

推荐答案

我在对 类似的问题.该解决方案涉及使用附加属性和一个框架,用于将接口命令从 ViewModel 传递回视图.

I have documented a "pure MVVM" way to do this in my answer to a similar problem. The solution involves using Attached Properties and a framework for passing interface commands from the ViewModel back to the View.

这篇关于MVVM 焦点到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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