How to set focus to a control with Caliburn.Micro MVVM
Asked Answered
S

2

8

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?

Stanchion answered 31/12, 2010 at 17:8 Comment(0)
N
9

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");
}
Narration answered 2/2, 2011 at 22:49 Comment(4)
For some reason the ActionExecutionContext parameter on method Execute() is always null. To solve this, I'm calling the coroutine after OnViewLoaded(object view) virtual method, then I create a new ActionExecutionContext() { View = (DependencyObject)view });Wellturned
@JohnPolvora not sure why that would be, how are you invoking the coroutine that uses this IResult?Narration
In a viewmodel derived from Screen base class, first I tried to invoke the IResult overriding OnActivate. But at OnActivate it seems that the view is not available for the execution context of coroutine. I call using Coroutine.BeginExecuteWellturned
@JohnPolvora Yeah OnActivate is not invoked as a coroutine by the framework, your solution will work fine, I add a virtual ViewLoaded coroutine to my ScreenBase and then invoke that via an Action from views that require it, just saves having to create the context yourself.Narration
C
-1

There is an easier way.

1º In the ViewModel add property _view as your UserControl

2º You must override OnViewLoaded of your ViewModel and set _view to View object.

3º Set focus from any method.

    public UserControlView _view { get; set; }

    protected override void OnViewLoaded(object view)
    {
        base.OnViewLoaded(view);
        _view = (UserControlView)view;

    }
    public void SetFocus()
    {
        _view.TextBox1.Focus();
    }

I hope help you.

Collegiate answered 13/1, 2023 at 15:2 Comment(1)
This breaks the separation between ViewModel and View which is not what you want to do.Existential

© 2022 - 2024 — McMap. All rights reserved.