0

In UWP, I want to handle and skip the tab key navigation for particular set of controls dynamically.

For e.g., I've two user controls (Both have lot of children which will be added dynamically) in my main page and want to skip tab key navigation for one usercontrol dynamically on specific scenario for a moment.

So I've tried to set "IsTabStop" as false to that UserControl. But which is not effective on its child controls. Still tab key focus moved inside the children of that UserControl.

Note: If I set "IsEnabled" as false, then its working. But I don't want to use because it affects Visual appearance.

Thanks in advance.

Selvamz
  • 362
  • 3
  • 16

1 Answers1

0

There is no IsTabStop property in StackPanel class. The IsTabStop property is defined in Windows.UI.Xaml.Controls.Control class, and the StackPanel class is not inherited from Control class, either directly or indirectly. You can not use IsTabStop property to set a StackPanel instance.

Therefore, if you want to skip tab key navigation for one stackpanel, you need to set the IsTabStop property to False in each control in the stackpanel.

Update:

By testing, the child elements in a UserControl can not inherit the value of IsTabStop property. Therefore, you cannot skip tab key navigation for all the child elements in a UserControl by setting the IsTabStop property to False.

You could use a method defind in your UserControl class to set IsTabStop to false for every item in your UserControl.

For example:

MyUserControl.cs

public void SetIsTabStop(bool flag)
{
    var result = VisualTreeFindAll<Control>(this);
    foreach (var item in result)
    {
        item.IsTabStop=flag;
    }
}
private IList<T> VisualTreeFindAll<T>(DependencyObject element)
            where T : DependencyObject
{
    List<T> retValues = new List<T>();
    var childrenCount = VisualTreeHelper.GetChildrenCount(element);
    for (var i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(element, i);
        var type = child as T;
        if (type != null)
        {
            retValues.Add(type);
        }
        retValues.AddRange(VisualTreeFindAll<T>(child));
    }
    return retValues;
}

Use the name of a UserControl instance to call the SetIsTabStop(bool flag) method with False.

userControlName.SetIsTabStop(false);

Update:

Maybe you could try the following workaround.

Add the KeyDown event handler for the UserControl in your Page.

 private void UserControl_KeyDown (object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Tab)
        {
            e.Handled = true;
            button1.Focus(FocusState.Keyboard);
        }
    }

You need to let the first control except the UserControl and all its child controls get the focus, then the key navigation will skip the child controls in the UserControl except the first child control in the UserControl.

YanGu
  • 3,006
  • 1
  • 3
  • 7
  • Sorry. I reframed my question. Actually I've UserControl instead of StackPanel and that usercontrol has lot of child controls which will be added dynamically. So setting IsTabStop as false to each & every child controls is not possible in our application scenario. Is there any solution handle IsTabStop in parent user control level? – Selvamz Nov 03 '20 at 10:24
  • I update the answer. You could view if the update meets your requirements. – YanGu Nov 04 '20 at 08:09
  • I already thought about this approach (updating all visual child controls). But our Parent UserControl has lot & lot child controls. So traversing entire Visual tree will impact performance heavily. Is there any other effective way to handle this? – Selvamz Nov 04 '20 at 13:43
  • I update the answer. You could view if the update meets your requirements. – YanGu Nov 05 '20 at 02:44
  • Has your problem been solved? If it is not solved, please feel free to contact us. – YanGu Nov 26 '20 at 09:19