I have a View that has a single TextBox
and a couple Button
s below it. When the window loads I want that TextBox
to have focus.
After having a 'WPF Initial Focus Nightmare' and based on some answers on stack, the following proved for me to be the best solution.
First, add your App.xaml OnStartup() the followings:
EventManager.RegisterClassHandler(typeof(Window), Window.LoadedEvent,
new RoutedEventHandler(WindowLoaded));
Then add the 'WindowLoaded' event also in App.xaml :
void WindowLoaded(object sender, RoutedEventArgs e)
{
var window = e.Source as Window;
System.Threading.Thread.Sleep(100);
window.Dispatcher.Invoke(
new Action(() =>
{
window.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
}));
}
The threading issue must be use as WPF initial focus mostly fails due to some framework race conditions.
I found the following solution best as it is used globally for the whole app.
Hope it helps...
Oran
In this case I think that it is fine to put the code into the view. Setting focus to a control affects the behaviour of the user interface rather than logic of the application and therefore is the responsibility of the view.
Actually, isn't focus a UI concern? MVVM is about separating concerns - what belongs to model is in model, what belongs to view is in view, and what bind model and view together is in ViewModel (this is of course oversimplified description).
This means, that UI logic remains in View - TextBox.Focus() is, in my opinion, appropriate way to make this happen.
Use the FocusManager to bind to that property.
<Window FocusManager.FocusedElement="{Binding ElementName=ViewModel.FocusedItem}"/>
Your ViewModel is a translator which exists solely to provide information to the View, so you can add whatever information to the VM that the View needs to function.
If it makes you feel better (it makes me feel better) you can do this in Xaml using an attached property:
http://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.focusedelement.aspx
Anything you can do in codebehind you can do in Xaml if you know the tricks. Fortunately, you didn't have to implement this trick - MS did it for you.
I'd consider the control with focus to be very much "visual only", so wouldn't have any problem with that being in code behind.
The idea of a VM is to move logic away from the View, and provide a databinding friendly version of your model for the view to bind to. This doesn't necessarily mean that all code should live in the VM, just the logic code and anything that isn't directly tied to the UI.