title: Navigate properly between tab in a WPF MVVM application
body: <p>I'm doing a WPF application with the M-V-VM patern (i'm using galasoft if it's relevant), but I have issues when I navigate through a tabcontrol.<br>
  I'm adding tabs on the run. All the binding seems to goes well : inside the tab or in the header of the tab.
  I've bind my tabcontrol to a observable list. Through an interface I'm adding several types of viewmodel to this list and the binding seems correct.<br>
  My XAML code looks like this :</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;Grid.Resources&gt;
          &lt;DataTemplate x:Key="itemTemplate"&gt;
              &lt;TechnicalControls:ItemTab /&gt;
          &lt;/DataTemplate&gt;
      &lt;/Grid.Resources&gt;
  
      &lt;TabControl Grid.Row="1" x:Name="MainTab" Grid.Column="1" 
              ItemsSource="{Binding TabViewModels}"
              SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
  
              ItemContainerStyleSelector="{StaticResource LastItemStyleSelector}"
              ItemTemplate="{StaticResource itemTemplate}"
              &gt;
  
          &lt;TabControl.Resources&gt;
              &lt;DataTemplate DataType="{x:Type VM:JobViewModel}" x:Shared="False" &gt;
                  &lt;FunctionnalControls:Job /&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type VM:ExcelJobViewModel}" x:Shared="False"&gt;
                  &lt;FunctionnalControls:ExcelJob /&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type VM:MonitoringViewModel}" x:Shared="False"&gt;
                  &lt;FunctionnalControls:Monitoring /&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate DataType="{x:Type VM:ErrorViewModel}" x:Shared="False"&gt;
                  &lt;FunctionnalControls:Error /&gt;
              &lt;/DataTemplate&gt;                
  
          &lt;/TabControl.Resources&gt;
      &lt;/TabControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>For example if I go from a ExcelJob to another ExcelJob usercontrol, the new usercontrol is not load properly but it changes then it works, for exemple, I can go to a ExcelJob to another ExcelJob if only I go through the monitoring.  </p>
  
  <p>I've already look to this <a href="https://stackoverflow.com/questions/601826/multiple-usercontrol-instances-in-tabcontrol">this</a> but it didn't work for me.<br>
  I've also looked at <a href="https://stackoverflow.com/questions/6538364/wpf-custom-textbox-takes-two-tabs-to-get-to-text">this</a> : it says that we should not used inputs because you can focus them. I've tried to set the IsEnabled property on the users controls to false. I did it when tabs were changing. It didn't work...  </p>
  
  <p>The only solution that I can see is to go through another a new usercontrol with no other purpose to be used every time a tab is changed but this is ugly, and I'm pretty sure, Microsoft thought about this and came up with a better solution.</p>
  
  <p>If necessary I can put the code of the view model.</p>
  
  <p>EDIT : Just to clarify, when I click on other tab with the same control, instead of showing me the new usercontrol, it shows me the previous one. In order to see the new one, I have to change to another tab with another usercontrol then come back on the one I want to see.<br>
  I've look through debug and the when I click on the other tab It doesn't call the viewmodel </p>
  
  <pre><code>&lt;UserControl x:Class="App.ExcelJob"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               DataContext="{Binding Main.ExcelJobVM, Source={StaticResource Locator }}"&gt;
      &lt;Grid &gt;
          &lt;Label&gt;Futur Excel Job&lt;/Label&gt;
          &lt;TextBox Width="200" Height="60" Text="{Binding Header}"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>So Main returns the Mainviewmodel and Main.ExcelJobVM returns the good viewmodel of the usercontrol. the returned isntance is based on selected Index.<br>
  The only thing I need is to force the redrawing of the usercontrol or recall the method to update the datacontext, by loading the good viewmodel. I tried, I've failed so far. I'm not sure of what I'm doing because I want to use the event SelectionChanged of the tabcontrol but it would be in the code behind, and I don't know if it would still respect the MVVM pattern.</p>