# Using Controls and Programmatic Focus to Bypass Blocks of Content in Silverlight

ID: SL25

Technology: silverlight

Type: Technique

## When to Use

- Microsoft Silverlight, versions 3 and greater
- Silverlight managed programming model and Silverlight XAML

## Description

The objective of this technique is to use the combination of Silverlight control activation and programmatic focus to enable the user to skip regions of content in a Silverlight application user interface.

The control that the user activates should clearly indicate that its purpose is to skip content, so that the resulting programmatic focus shift is not interpreted as an undesired change of context.

The object to call focus to (the receiver of focus after the user-initiated action is triggered) has to be a `Control` in the Silverlight programming model. This is because the Focus method must be called on the target, and therefore the target must inherit the `Control` class. So, an application author might call focus to a read-only `TextBox`, or perhaps a `RichTextBox`, depending on the purpose of the Silverlight application and its user interface design. You can also focus a `UserControl`, for cases where the area to call focus to represents a custom control implementation.

#### Setting TabIndex (not recommended)

Silverlight provides a `TabIndex` attribute that can be used to override the default-generated tab sequence. Do not attempt to adjust tab index as a technique for getting past content blocks. Doing so will create a focus order that does not match the apparent visual order, as described in SC 2.4.3.

## Examples

### User-enabled control that programmatically sets focus

The following is the XAML for the user interface.

       <StackPanel Name="LayoutRoot">
           <Button Name="bypassbtn1" Click="bypassbtn1_Click">Skip menus, go to main page content</Button>
           <!intervening content-->
           <StackPanel>
               <RichTextBox Name="rtb_MainContent" IsReadOnly="True">
               <Paragraph>Here is the main content ....</Paragraph>
               </RichTextBox>
           </StackPanel>
       </StackPanel>

The following is the event handler that forces focus.

           private void bypassbtn1_Click(object sender, RoutedEventArgs e)
           {
               rtb_MainContent.Focus();
           }

This example is shown in operation in the working example of Programmatic Focus.

## Tests

### Procedure

1. Open the test HTML page for a Silverlight application.
2. Check for a control that indicates that activating that control can skip to some particular region of the content.
3. Activate that control. Verify that activating the control causes focus to go to that region, and that a repeated block or blocks of content are skipped.

### Expected Results

#2 and #3 are true.

## Related Techniques

- G123

## Resources

- Focus Overview
- Silverlight Accessibility Overview