Thursday, August 12, 2010

Silverlight ChildWindows and Focus

ChildWindow is an awesome control.  No doubt about that.  It saves me a lot of time and effort creating my own.  But it's not without it's quirks.

I can't figure out exactly when, but a ChildWindow will somehow .Focus() itself, after the constructor and after the ChildWindow_Loaded event. This was driving me crazy. I'd focus my TextBox in the Loaded event, see the cursor blink once, and then vanish.

So here's a clunky hack to get around it:
private bool firstFocus = true;
//...
private void ChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
    if (firstFocus)
    {
        textBox1.Focus();
        firstFocus = false;
    }
}

Credit where credit is due.

No comments:

Post a Comment

Speak your mind.