如何将焦点设置与Caliburn.Micro MVVM控制 [英] How to set focus to a control with Caliburn.Micro MVVM

查看:183
本文介绍了如何将焦点设置与Caliburn.Micro MVVM控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,我想将焦点设置到文本框,当特定用户操作发生。我知道做事的方式MVVM是绑定到虚拟机的属性,但文本框不具有的特性,将允许这种情况发生。什么是设置为从VM焦点的最好方法是什么?

I have a form and I want to set the focus to a text box when certain user actions happen. I know the MVVM way of doing things is to bind to VM properties, however the TextBox does not have a property that will allow this to happen. What's the best way to set the focus from the VM?

推荐答案

我已经创建了一个IResult实现,它工作得很好实现这一目标。你可以从IResult,然后你就可以搜索(我按名称搜索)为您想要对焦的控制ActionExecutionContext的看法。

I have created an IResult implementation that works quite well for achieving this. You can get the view from the ActionExecutionContext of the IResult, which you can then search (I search by name) for the control you want to focus.

public class GiveFocusByName : ResultBase
{
    public GiveFocusByName(string controlToFocus)
    {
        _controlToFocus = controlToFocus;
    }

    private string _controlToFocus;

    public override void Execute(ActionExecutionContext context)
    {
        var view = context.View as UserControl;


        // add support for further controls here
        List<Control> editableControls =
                view.GetChildrenByType<Control>(c => c is CheckBox ||
                                                      c is TextBox ||
                                                      c is Button);

        var control = editableControls.SingleOrDefault(c =>
                                                 c.Name == _controlToFocus);

        if (control != null)
        control.Dispatcher.BeginInvoke(() =>
        {
            control.Focus();

            var textBox = control as TextBox;
            if (textBox != null)
                textBox.Select(textBox.Text.Length, 0);
        });

        RaiseCompletedEvent();
    }
}

我ommitted一些额外的代码来获得视图背景视图是<$ C $ 。C> ChildWindow 如果你需要我可以提供

I have ommitted some extra code to get the view from the context when the view is a ChildWindow I can provide if you require.

另外GetChildrenByType是一个扩展方法,这里是野生许多可用实现之一:

Also GetChildrenByType is an extension method, here is one of many implementations available in the wild:

public static List<T> GetChildrenByType<T>(this UIElement element,
                          Func<T, bool> condition) where T : UIElement
{
    List<T> results = new List<T>();
    GetChildrenByType<T>(element, condition, results);
    return results;
}

private static void GetChildrenByType<T>(UIElement element,
                          Func<T, bool> condition, List<T> results) where T : UIElement
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
    {
        UIElement child = VisualTreeHelper.GetChild(element, i) as UIElement;
        if (child != null)
        {
            T t = child as T;
        if (t != null)
        {
            if (condition == null)
                results.Add(t);
            else if (condition(t))
            results.Add(t);
        }
        GetChildrenByType<T>(child, condition, results);
        }
    }
}

您的动作然后将东西像下面的(援引Caliburn.Micro ActionMessage的风格)

Your action would then be something like the following (invoked in Caliburn.Micro ActionMessage style).

public IEnumerable<IResult> MyAction()
{
    // do whatever
    yield return new GiveFocusByName("NameOfControlToFocus");
}

这篇关于如何将焦点设置与Caliburn.Micro MVVM控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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