How to set focus to a control with Caliburn.Micro MVVM

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:17:23

问题


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?


回答1:


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();
    }
}

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.

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);
        }
    }
}

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");
}


来源:https://stackoverflow.com/questions/4571328/how-to-set-focus-to-a-control-with-caliburn-micro-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!