WPF: how to set focus to a textbox at startup

J

Jeremy

I have a wpf window that contains a couple of text boxes. I want a certain
one of these to have focus at startup. IsFocused is read-only; I've set the
tabstop to 0; at startup, the focus is somewhere on the form (no visible
sign of what it might be). How do I do this very, very simple thing?
 
C

Chris Jobson

I have a wpf window that contains a couple of text boxes. I want a certain
one of these to have focus at startup. IsFocused is read-only; I've set
the tabstop to 0; at startup, the focus is somewhere on the form (no
visible sign of what it might be). How do I do this very, very simple
thing?

<Window
....
FocusManager.FocusedElement="{Binding ElementName=myTextBox}">
....
<TextBox Name="myTextBox" />
....
</Window>

Chris Jobson
 
J

Jeremy

<Window
...
FocusManager.FocusedElement="{Binding ElementName=myTextBox}">
...
<TextBox Name="myTextBox" />
...
</Window>

Chris I'll try that. But isn't there a way to control this through the code
behind? What if I want to focus different controls depending on what the
user does?

Jeremy
 
C

Chris Jobson

<Window
Chris I'll try that. But isn't there a way to control this through the
code behind? What if I want to focus different controls depending on what
the user does?

Even easier!
myTextBox.Focus();
If you want to decide which control to give focus to when the form first
appears, put the code in the Loaded event.

Regards
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top