Setting default focus to a particular control via XAML

I wanted to avoid calling control.Focus() in the Window derived class’s constructor and wanted to see how it’s done via XAML. I couldn’t get it working after 5-10 minutes of trying out various stuff, but someone helped me out in the MSDN forums. Here’s how it’s done – in the following code snippet, I am setting initial focus to a ListBox control named listBox1.

<Grid FocusManager.FocusedElement="{Binding ElementName=listBox1}">

3 thoughts on “Setting default focus to a particular control via XAML

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s