How to programmatically select a TabItem in WPF TabControl

Pierre Toutant picture Pierre Toutant · Oct 28, 2011 · Viewed 61.2k times · Source

I would like to know how to select a specific TabItem in a WPF TabControl.

I tried these bellow but nothing work!

MyTabControl.SelectedIndex = x

MyTabControl.SelectedItem = MyTabItem

MyTabControl.SelectedValue = MyTabItem

MyTabItem.IsSelected = True

Answer

Adrian Ratnapala picture Adrian Ratnapala · Sep 21, 2014

As @Chris says, any of the first three things should work and as @Phyxx says, it doesn't always really work. The problem is some subtle thing about the order of property changes. To work around it you need to let the WPF invoke your tab-selection code in its own time:

Dispatcher.BeginInvoke((Action)(() => MyTabControl.SelectedIndex = x));

This does just what Phyxx' timer does, but in a slightly less extreme way.