title: Get a property of a tabitem's viewmodel when it's selected
body: <p>I have a <code>UserControl</code> <strong>ParentView</strong> containing a <code>DataGrid</code> and a <code>catel:TabControl</code>, in which each <code>TabItem</code> is a tiny <code>UserControl</code> and whose <strong>ViewModel</strong> is a <strong>subclass of a BaseTabViewModel</strong>.</p>
  
  <p>This <code>BaseTabViewModel</code> contains a <code>ObservableCollection</code> of a custom class, whose properties will generate the columns of the <code>DataGrid</code>.</p>
  
  <p><strong>Since I have only one DataGrid for all tabs, how can I fill it with the ObservableCollection of selected tab's viewmodel automatically?</strong></p>
  
  <p><img src="https://i.stack.imgur.com/UMYMu.png" alt="enter image description here"></p>
  
  <p>Right now, I just unload tabs (<code>LoadTabItems="SingleUnloadOthers"</code>) and, since every tab's viewmodel regenerate its collection when instanciated, I use InterestedIn and OnViewModelPropertyChanged to get it; yet this unloading behavior isn't completely reliable, and I often find myself with the DataGrid still filled by the previous tab instead of being cleaned when changing tab.</p>
  
  <p>To note, the fact that it gets cleaned isn't a desired behavior, I just have no other choice right now. Having each tab remaining loaded is my goal.</p>
  
  <p><em>I know inheritance is pretty frowned upon when it comes to viewmodels, but I honestly can't think of a better way to do it.</em></p>
  
  <hr>
  
  <p><strong>ParentView</strong></p>
  
  <pre><code>&lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;catel:TabControl LoadTabItems="SingleUnloadOthers"&gt;
          &lt;TabItem Header="FIRST"&gt;
              &lt;local:FirstView/&gt;
          &lt;/TabItem&gt;
          &lt;TabItem Header="SECOND"&gt;
              &lt;local:SecondView/&gt;
          &lt;/TabItem&gt;
          &lt;TabItem Header="THIRD"&gt;
              &lt;local:ThirdView/&gt;
          &lt;/TabItem&gt;
          &lt;TabItem Header="FOURTH"&gt;
              &lt;local:FourthView/&gt;
          &lt;/TabItem&gt;
      &lt;/catel:TabControl&gt;
  
      &lt;DataGrid Grid.Row="1" IsReadOnly="True" ItemsSource="{Binding Fields}"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>ParentViewModel</strong></p>
  
  <pre><code>[InterestedIn(typeof(FirstViewModel))]
  [InterestedIn(typeof(SecondViewModel))]
  [InterestedIn(typeof(ThirdViewModel))]
  [InterestedIn(typeof(FourthViewModel))]
  public class ParentViewModel : ViewModelBase
  {
      public ObservableCollection&lt;Field&gt; Fields
      {
          get { return GetValue&lt;ObservableCollection&lt;Field&gt;&gt;(FieldsProperty); }
          set { SetValue(FieldsProperty, value); }
      }
      public static readonly PropertyData FieldsProperty = RegisterProperty("Fields", typeof(ObservableCollection&lt;Field&gt;));
  
      protected override void OnViewModelPropertyChanged(IViewModel viewModel, string propertyName)
      {
          if (propertyName.Equals("Fields"))
          {
              BaseParserViewModel p = viewModel as BaseParserViewModel;
              Fields = p != null ?? p.Fields;
          }
          base.OnViewModelPropertyChanged(viewModel, propertyName);
      }
  }
  </code></pre>
  
  <p><strong>BaseTabViewModel</strong></p>
  
  <pre><code>public abstract class BaseTabViewModel : ViewModelBase
  {
      protected BaseParserViewModel()
      {
          Fields = new ObservableCollection&lt;Field&gt;();
      }
  
      public ObservableCollection&lt;Field&gt; Fields
      {
          get { return GetValue&lt;ObservableCollection&lt;Field&gt;&gt;(FieldsProperty); }
          set { SetValue(FieldsProperty, value); }
  
      public static readonly PropertyData FieldsProperty = RegisterProperty("Fields", typeof(ObservableCollection&lt;MappedField&gt;));
  
  }
  </code></pre>
  
  <p>BaseTabViewModel's childs have nothing of interest, as they don't interact with their parent, except for some <code>[ViewModelToModel]</code> properties.</p>