I have an ObservableCollection which I would bind to the ItemsSource and I have a property type of Tag which I will bind to the SelectedValue. Whenever I want to change the selected tab, I just set the selected value. Here's my dirty code. I hope this will help you with your problem.
<Window x:class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication9"
Title="MainWindow" Height="350" Width="525">
<stackpanel>
<tabcontrol itemssource="{Binding TagCollection}" selectedvalue="{Binding SelectedTag}">
<tabcontrol.itemtemplate>
<datatemplate>
<textblock text="{Binding Name}" />
</datatemplate>
</tabcontrol.itemtemplate>
</tabcontrol>
<button content="Test" command="{Binding ClickCommand}" />
</stackpanel>
</window>
Here's the view model which I set to the data context of the view.
I created a click commant which will be triggered when you click the button. This will let you set the focus to TagCollection[1] (in a 0 based index it's the 2nd tab). Now the tricky part here is that you have to call raisepropertychange method on the setter. This will tell the view that someone set a new value for the SelectedTag and the view should update.
public class TagVM: ViewModelBase
{
ObservableCollection<tag> tagCollection;
public TagVM()
{
tagCollection = new ObservableCollection<tag>();
tagCollection.Add(new Tag() { Name = "two", Priority = "2" });
tagCollection.Add(new Tag() { Name = "another 2", Priority = "2" });
tagCollection.Add(new Tag() { Name = "three", Priority = "3" });
tagCollection.Add(new Tag() { Name = "one", Priority = "1" });
}
private Tag selectedTag;
public Tag SelectedTag
{
get { return selectedTag; }
set
{
selectedTag = value;
base.RaisePropertyChanged("SelectedTag");
}
}
public ObservableCollection<tag> TagCollection
{
get { return tagCollection; }
set
{
tagCollection = value;
base.RaisePropertyChanged("TagCollection");
}
}
public ICommand ClickCommand
{
get
{
return new RelayCommand(() => { SelectedTag = TagCollection[1]; });
}
}
}