Skip to main content

Microsoft Silverlight

Answered Question Bug: Can not programmatically focus controls in ScrollViewerRSS Feed

(0)

jseaver
jseaver

Member

Member

14 points

14 Posts

Bug: Can not programmatically focus controls in ScrollViewer

I have wrapped a ScrollViewer around a 'form' that contains a number of textboxes.  I am unable to programmatically set the focus to the textboxes using something like Textbox1.focus();

I need to be able to move focus from textbox to textbox as the user performs certain actions.

If I remove the ScrollViewer it works fine.

Although the focus method returns true, focus has not been set to a textbox.  I assume it is being intercepted somehow by the ScrollViewer.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Bug: Can not programmatically focus controls in ScrollViewer

Hello, can you post some code? I can't reproduce this issue. This is what I tried:

<ScrollViewer x:Name="LayoutRoot" Background="White">

<StackPanel>

<TextBox x:Name="t1"/>

<TextBox x:Name="t2"/>

<Button Content="Change Focus" Click="Button_Click"/>

</StackPanel>

</ScrollViewer>

private void Button_Click(object sender, RoutedEventArgs e)

{

t2.Focus();

}

The second TextBox successfully got focused after clicking the Button.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

jseaver
jseaver

Member

Member

14 points

14 Posts

Re: Bug: Can not programmatically focus controls in ScrollViewer

Try it with something other than a button.   Add the following code below the button listed in your sample:

<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">

<TextBlock Text="Click me" />

</Grid>

 

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Re: Bug: Can not programmatically focus controls in ScrollViewer

Well, ScrollViewer handles MouseLeftButtonDown and changes focus internally:

private void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
    if (!e.Handled && base.Focus())
    {
        e.Handled = true;
    }
}

So either set e.Handled = true in your own MouseLeftButtonDown event handler, or handle MouseLeftButtonUp instead.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities