WPF tab index issue

961 views Asked by At

I'm working on an application where a user defines a the controls on a form and can set the tab index of any control. As each control is added to the Grid that comprises the viewable form area, the tab index is set to either 0 (default) or some user-defined tab index. Tabbing through the form works fine until the tabindex of one of the controls is changed at runtime(the index doesn't seem to matter.) After this, tabbing cycles only through some of the controls and in addition, the window menu items are now tab stops(they weren't prior to the tabindex change.) Also, the menu's tab properties aren't bound to any datacontext.

The control that's currently changed is a checkbox, but I'm unable to reproduce the behavior with a simplified layout, so any suggestions would help.

1

There are 1 answers

0
jchristof On BEST ANSWER

Our "form pages" user controls invisible and beneath the current visible page were never disabled when new ones were pushed on the top. Therefore they were included in the tab indexing behavior causing unwanted side effects.

This helped me get to the bottom of the issue:

void InitializeFocusLogger() {
        //debug key logging to make focus bugs easier to track
        EventManager.RegisterClassHandler(
            typeof(UIElement),
            Keyboard.PreviewGotKeyboardFocusEvent,
            (KeyboardFocusChangedEventHandler)OnPreviewGotKeyboardFocus);
}

string lastID = string.Empty;
private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)         {
    FrameworkElement control = e.NewFocus as FrameworkElement;
    if (control == null) return;

    ControlViewModel controlVM = control.DataContext as ControlViewModel;

    if (controlVM == null || lastID == controlVM.ID) return;

    lastID = controlVM.ID;

    Debug.Print("Focused: {0} TabIndex: {1}", controlVM.ID, controlVM.TabIndex);
}