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

前端 未结 1 1871
情话喂你
情话喂你 2021-02-06 07: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 do

相关标签:
1条回答
  • 2021-02-06 08:07

    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");
    }
    
    0 讨论(0)
提交回复
热议问题