XAML:
<TabControl x:name="MyTabControl">...</TabControl>
Code Behind:
public MyUserControlorViewConstructor() { InitializeComponent(); MyViewModel mvm = new MyViewModel(); DataContext = mvm; mvm.MyTabControl = MyTabControl; }
ViewModel:
public TabControl MyTabControl { get; set; } public static void SetSelectedTab(string tabName) { for (int i = 0; i < MyTabControl.Items.Count; i++) { TabItem item = MyTabControl.Items.GetItemAt(i) as TabItem; if (null == item || (item.Name != tabName && item.Header.ToString() != tabName)) continue; MyTabControl.SelectedIndex = i; item.IsSelected = true; return; } } public static void SetSelectedTab(int tabIndex) { TabItem item = MyTabControl.Items.GetItemAt(tabIndex) as TabItem; if (item == null) return; item.IsSelected = true; MyTabControl.SelectedIndex = tabIndex; }