Dienstag, 8. Februar 2011

Silverlight set initial Focus / Focus popup or ChildWindow

In our current project we had the problem, that we cannot set the Focus to a Control in an initial Login-Dialog (ChildWindow). We try some things like setting the Focus after the Loaded- or PageUpdated-Event – but nothing helps.

After some time of additional research we found out, that the original problem was the missing Focus of the Silverlight Plugin after starting the Silverlight application.

So first step we do this (“HtmlPage.Plugin.Focus” is the magic):

var loginView = new LoginRegistrationView();
loginView.Show();
 
HtmlPage.Plugin.Focus();

After this, we can use the GotFocus-Event of our Login-Form to set the Focus to the UserName-Field of the DataForm at our Login-Form-ChildWindow.

public LoginForm()
{
    InitializeComponent();
    
    this.GotFocus += this.LoginFormGotFocus;
}
private void LoginFormGotFocus(object sender, System.Windows.RoutedEventArgs e)
       {
           if (this.firstCall)
           {
               var userNameTextBox = (TextBox)loginDataForm.Fields["UserName"].Content;
               userNameTextBox.Focus();
               this.firstCall = false;
           }
       }

Keine Kommentare:

Kommentar veröffentlichen