Introduction
Today I will be writing something about focusing any element in Silverlight. Let's suppose we have a login panel as the very first screen and after validation we will navigate to another page. So when I am talking about the login panel we will have two input fields (textboxes) to insert username and password and looking from thge user's point of view we want that the username textbox field is to be focused.
Setting Up Silverlight Project
Let's create a Silverlight application and name it "FocusingControlInSilverlightApplication". Let's use one textbox for username, one passwordbox for password and one login button. After designing, it will look something like the following:

So here comes the question; how to set focus to the username textbox.
So as we know we have a function something like Focus() to make it focused but it is not going to work for the very first time. Here is the reason, when we will run the application the Silverlight Plugin is not yet focused. Unless until we focus that Plugin in we can't focus any control in Silverlight application. To make it happen we have to simply add a single line of code on the loaded event which will look like and focus the Plugin.
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//First focus the silverlight plogin and than focus the control.
HtmlPage.Plugin.Focus(); (1)
userNameTextBox.Focus();(2)
}
-
Line one is focusing the Silverlight Plugin.
-
Focus that element.
But we need to add the following to the namespace import section.
using System.Windows.Browser;
Now run the application and you will find the control focused.





Himanshu SahuPosted Jan 24, 2012, 1:45 AM
Nice Presentation Mukesh :)
Emmy DickensPosted Jan 24, 2012, 12:18 AM
Well explaination...
Stephen JohnsonPosted Jan 23, 2012, 11:50 PM
Hi Mukesh. You have presented your article very nicely.