views:

11423

answers:

10

In my Silverlight application, I can't seem to bring focus to a TextBox control. On the recommendation of various posts, I've set the IsTabStop property to True and I'm using TextBox.Focus(). Though the UserControl_Loaded event is firing, the TextBox control isn't getting focus. I've included my very simple code below. What am I missing? Thanks.

Page.xaml

<UserControl x:Class="TextboxFocusTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Loaded="UserControl_Loaded" 
    Width="400" Height="300">

    <Grid x:Name="LayoutRoot" Background="White">        
        <StackPanel Width="150" VerticalAlignment="Center">            
            <TextBox x:Name="RegularTextBox" IsTabStop="True" />    
        </StackPanel>        
    </Grid>
</UserControl>

Page.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace PasswordTextboxTest
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            RegularTextBox.Focus();
        }
    }
}
+1  A: 

You code to set the focus is correct since if you add a button that calls the same code it works perfectly:

<StackPanel Width="150" VerticalAlignment="Center">
    <TextBox x:Name="RegularTextBox" IsTabStop="True" />
    <Button Click="UserControl_Loaded">
        <TextBlock Text="Test"/>
    </Button>
</StackPanel>

So I'm assuming this is something to do with Focus() requiring some kind of user interaction. I couldn't get it to work with a MouseMove event on the UserControl, but putting a KeyDown event to set the focus works (although the template doesn't update to the focused template).

Width="400" Height="300" Loaded="UserControl_Loaded" KeyDown="UserControl_KeyDown">

Seems like a bug to me....

Bryant
Thanks Bryant. I like how you validated using the button click. I'll see if I can't come up with something similar to your MouseMove suggestion.
Ben Griswold
+2  A: 

Are you sure you're not really getting focus? There's a known bug in Beta 2 where you'll get focus and be able to type but you won't get the caret or the border. The workaround is to call UpdateLayout() on the textbox right before you call Focus().

Bill Reiss
Good question, but I'm sure. Focus isn't on the TextBox after the UserControl loads. Strange.
Ben Griswold
A: 

I forgot one thing...I haven't found a way to force focus to your Silverlight application on the page reliably (it will work on some browsers and not on others).

So it may be that the Silverlight app itself doesn't have focus. I usually trick the user into clicking a button or something similar before I start expecting keyboard input to make sure that the silverlight app has focus.

Bill Reiss
+1  A: 

I would try adding a DispatcherTimer on the UserLoaded event that executes the Focus method a few milliseconds after the whole control has loaded; maybe the problem is there.

Santiago Palladino
Thanks for the suggestion. I hooked up the DispatcherTimer and result is the same. It isn't until I physically take an action (click on the page, for example) is focus set.
Ben Griswold
+10  A: 

I found this on silverlight.net, and was able to get it to work for me by adding a call to System.Windows.Browser.HtmlPage.Plugin.Focus() prior to calling RegularTextBox.Focus():

   private void UserControl_Loaded(object sender, RoutedEventArgs e)
   {        
      System.Windows.Browser.HtmlPage.Plugin.Focus();
      RegularTextBox.Focus();
   }
Jim B-G
That did the trick. It does not provide an indicator that the textbox has focus (carat within the textbox or border highlighting) but I am happy. Thanks.
Ben Griswold
"System.Windows.Browser.HtmlPage.Plugin.Focus()"what does it do?
Amby
as name suggest System.Windows.Browser.HtmlPage.Plugin.Focus() will focus the silverlight plugin in browser window.
bugBurger
+1  A: 

Calling System.Windows.Browser.HtmlPage.Plugin.Focus(); worked for me as well!

A: 

For out-of-browser apps the System.Windows.Browser.HtmlPage.Plugin.Focus(); doesn't exist.

See my question here for other ideas.

Simon_Weaver
+6  A: 

thanks Santiago Palladino Dispatcher worked for me perfectly. What I am doing is:

this.Focus(); then Dispatcher.BeginInvoke(() => { tbNewText.Focus();});

om
This worked for me, too.
kpozin
+5  A: 
Plugin.Focus(); 

didn't work for me.

Calling

 Dispatcher.BeginInvoke(() => { tbNewText.Focus();});

From the Load event worked.

rekle
A: 

I also needed to call

Deployment.Current.Dispatcher.BeginInvoke(() => myTextbox.Focus());

interestingly this call is happening inside an event handler when I mouseclick on a TextBlock, collapse the TextBlock and make the TextBox Visible. If I don't follow it by a dispatcher.BeginInvoke it won't get focus.

-Mike

mike gold