select tabItem programmatically in WPF

14,936

If you're forcing the IsSelected property, I'd also give the first TextBox a name and set the focus after you set the selected tab.

If you're building your UI dynamically, this won't work, but you can create a utility method which searches the logical tree (or the visual tree if you're using presenters/view-models) for the first input control and then set the focus.

Share:
14,936
Ghassan Karwchan
Author by

Ghassan Karwchan

Updated on June 19, 2022

Comments

  • Ghassan Karwchan
    Ghassan Karwchan almost 2 years

    I have different tabItems in a TabControl and each tabItem has some input fields.

    I am moving between the tabItems programmatically (like a wizard to move from the first to the next)

    I am using this code inside the "Next" button

    tabItem2.isSelected = true;

    my problem that when I move between the tabItems by clicking on them, the focus (keyboard focus) will move to the first textbox input.

    But programmatically with the previous code, the focus won't move to the first input textbox item inside the tabItem.

    Any idea?

  • HappyNomad
    HappyNomad over 13 years
    Or instead of calling textbox.Focus(), you could do what WPF does internally within its TabItem.OnPreviewGotKeyboardFocus method. That is, call tabitem.MoveFocus().