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.
If I was not using MVVM I would just call TextBox.Focus()
in the Loaded event. However my ViewModel does not know about my view so how can I accomplish this without putting code into the codebehind of my view?
EDIT: After reading the answers I decided to put this code in the view xaml
<DockPanel FocusManager.FocusedElement="{Binding ElementName=MessageTextBox}">
<TextBox Name="MessageTextBox" Text="{Binding Message}"/>
</DockPanel>
If this were anything other than initial page focus I would probably recommend Jon Galloway's answer since it can be controlled from the ViewModel.