What is a good practice of setting control focus in MVVM architecture.
The way I envision it, is with a property on the ViewModel that would trigger a focus change when needed. And than have the UI controls bind/listen to that property so that if it changes, appropriate focus will be set.
I see it as a ViewModel thing, because i want to set focus appropriate after a certain action was performed by the ViewModel, such as loading certain data.
What's the best practice?
The question has been asked a couple of times, unfortunately the answers only apply to WPF. So for silver light using MVVM, you can also bind with any property for detail please visit the following link
http://codenicely.blogspot.com/2012/01/how-to-set-textbox-focus-in-silverlight.html
You could introduce an interface for the View so that the ViewModel can tell the View to set the Focus. The BookLibrary sample application of the WPF Application Framework (WAF) shows how to do it. Please have a look at the BookListViewModel.
The ViewModel throws an event to the View telling it that the action has been completed, and the View will set the focus.
Use the IsFocused Attached Property as suggested in the Answer here: Set focus on textbox in WPF from view model (C#)
Then you can simply bind to a property in your viewmodel.
If you are using Caliburn.Micro, here is a service that I created to set Focus to any Control in the view inherited from Screen.
How to use this?
From your ViewModel inherited from Caliburn.Micro.Screen or Caliburn.Micro.ViewAware
this.SetFocus(()=>ViewModelProperty);
orthis.SetFocus("Property");
How it works?
This method will try to search for an element in the Visual Tree of View and focus will be set to any matching control. If no such control found, then it will make use of the BindingConventions used by Caliburn.Micro.
For ex.,
It will look for the Propery in the BindingExpression of the control. For TextBox, it will look whether this property is binded to Text property then the focus will be set.