1

I want to select tab from tab control after creating it.

XAML:

<TabControl SelectedIndex="{Binding SelectedTabIndex}" Name="Items">
      <TabControl.Resources>
      </TabControl.Resources>
</TabControl>

MainViewModel:

public int SelectedTabIndex
{
   get
   {
      return Items.Count - 1;
   }
   set { ; }
}
public void AddTab()
{
   var chart = new ChartViewModel(this.eventAggregator, this.windowManager);
   NotifyOfPropertyChange(() => SelectedTabIndex);
}

ChartViewModel is a class inherited from Caliburn.Micro.Screen, MainViewModel inherits from Caliburn.Micro.Conductor<Caliburn.Micro.Screen>.Collection.OneActive

The tab is created properly, but it is not selected after that.

Jaroslaw Matlak
  • 574
  • 1
  • 12
  • 23
  • 1
    Binding to SelectedIndex (or similar) is imho a code smell in wpf. It's always easier and cleaner to use ItemsSource/SelectedItem to manage selection. Also, for a cleaner example of how to use the TabControl in an MVVM way, see my answer here http://stackoverflow.com/a/5651542/1228 –  Dec 07 '16 at 15:22

1 Answers1

4

Change your xaml code to something like this,

<TabControl SelectedIndex="{Binding SelectedTabIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="Items">
      <TabControl.Resources>
      </TabControl.Resources>
</TabControl>
Vijay
  • 663
  • 4
  • 15