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?
Asked
Active
Viewed 4,325 times
2 Answers
8
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");
}

Simon Fox
- 10,409
- 7
- 60
- 81
-
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 });` – Jone Polvora Jun 05 '12 at 22:48
-
@JohnPolvora not sure why that would be, how are you invoking the coroutine that uses this IResult? – Simon Fox Jun 06 '12 at 03:08
-
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.BeginExecute – Jone Polvora Jun 06 '12 at 05:30
-
@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. – Simon Fox Jun 06 '12 at 23:12
-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.
-
This breaks the separation between ViewModel and View which is not what you want to do. – cederlof Feb 10 '23 at 14:07