如何设置焦点和选择所有的初始文本框(MVVM样式)? [英] How to set focus on AND SELECT ALL of an initial text box (MVVM-Style)?

查看:142
本文介绍了如何设置焦点和选择所有的初始文本框(MVVM样式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WPF页面,其中有一个文本框字段,当页面显示时,我的客户端需要突出显示。在后面的代码中,这将是三行,但我正在通过MVVM(我开始认为是有点过分评级)漫游。我已经尝试了许多不同的行为和全局事件变体,并且 FocusManager.FocusedElement ,但是我没有做任何事情可以做到。



最终,我使用的大部分代码都调用了这两行代码:
$ b

  Keyboard.Focus(textBox) ; 
textBox.SelectAll();

但是无论我把这些行放在哪里,没有文字被选中。我从来没有遇到过这么简单的麻烦。我一直在互联网上打了两个小时。有没有人知道如何做到这一点?



再次,我想要做的是有文本框焦点和文本所有选定的页面导航。请帮忙解决方法

/ strong>即可。

把代码放在背后。它不会中断MVVM分离。

  public void WhateverControl_Loaded(stuff)
{
Keyboard.Focus(textBox);
textBox.SelectAll();



$ b $ p
$ b

如果您需要这样做以响应特定的应用程序/业务逻辑。创建一个附加属性。



或者:

你的View是通过以下方式解析ViewModel的:

  this.DataContext as MyViewModel; 

然后在ViewModel中创建一些可以挂钩的事件:

  public class MyViewModel 
{
public Action INeedToFocusStuff {get; set;}
$ b $ public void SomeLogic
{
if(SomeCondition)
INeedToFocusStuff();




$ b $ p $然后在视图中将其挂钩:

  public void Window_Loaded(或者其他)
{
var vm = this.DataContext as MyViewModel;
vm.INeedToFocusStuff + = FocusMyStuff;

$ b $ public void FocusMyStuff()
{
WhateverTextBox.Focus();





$ b

看看这个简单的抽象如何在View和ViewModel相关的东西中保持View相关的东西在ViewModel中,同时允许它们进行交互。把事情简单化。你不需要美国国家航空航天局的服务器的WPF应用程序。




并没有MVVM不被高估,MVVM是非常有用的和我会说甚至是必要的。一旦开始使用ItemsControl,比如 ListBox es或 DataGrid s, p>

I have a simple WPF page with one text box field that my client wants highlighted when the page shows up. In code behind, it would be three lines, but I'm sogging through MVVM (which I'm starting to think is a little over-rated). I've tried so many different variants of behaviors and global events and FocusManager.FocusedElement, but nothing I do will do this.

Ultimately the most of the code I've been using calls these two lines:

Keyboard.Focus(textBox);
textBox.SelectAll();

But no matter where I put these lines the text box is only focused; no text is selected. I have never had this much trouble with something so simple. I've been hitting my head against the internets for two hours. Does anyone know how to do this?

Again, all I want to do is have the text box focus and it's text all selected when the page is navigated to. Please help!

解决方案

"Focus" and "Select All Text from a TextBox" is a View-specific concern.

Put that in code Behind. It does not break the MVVM separation at all.

public void WhateverControl_Loaded(stuff)
{
    Keyboard.Focus(textBox);
    textBox.SelectAll();
}

If you need to do it in response to a specific application/business logic. Create an Attached Property.

Or:

have your View resolve the ViewModel by:

this.DataContext as MyViewModel;

then create some event in the ViewModel to which you can hook:

public class MyViewModel
{
    public Action INeedToFocusStuff {get;set;}

    public void SomeLogic()
    {
        if (SomeCondition)
            INeedToFocusStuff();
    }
}

then hook it up in the View:

public void Window_Loaded(Or whatever)
{
    var vm = this.DataContext as MyViewModel;
    vm.INeedToFocusStuff += FocusMyStuff;
}

public void FocusMyStuff()
{
    WhateverTextBox.Focus();
}

See how this simple abstraction keeps View related stuff in the View and ViewModel related stuff in the ViewModel, while allowing them to interact. Keep it Simple. You don't need NASA's servers for a WPF app.


And no MVVM is not overrated, MVVM is extremely helpful and I would say even necessary. You'll quickly realize this as soon as you begin working with ItemsControls such as ListBoxes or DataGrids.

这篇关于如何设置焦点和选择所有的初始文本框(MVVM样式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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