title: WPF Caliburn.Micro and TabControl - changing tab, not changing model
body: <p>I'm kind of newbie in WPF&amp;MVVM&amp;Caliburn so I beg your indulgence :) </p>
  
  <p>I have a problem with binding TabControl with dynamically created models. Tabcontrol is being created correctly but changing tab is not switching viewmodel used to bind "view" (I'm using viewmodel first approach)</p>
  
  <p>I've made my solution basing on this question: <a href="https://stackoverflow.com/questions/20883868/wpf-caliburn-micro-and-tabcontrol-with-usercontrols-issue/20885027">WPF Caliburn.Micro and TabControl with UserControls issue</a></p>
  
  <p>This is my model definition:</p>
  
  <pre><code>public interface IMainScreenTabItem : IScreen
  {
  }
  
  public class MainViewTestTabsViewModel : Conductor&lt;IMainScreenTabItem&gt;.Collection.OneActive
  {
      public MainViewTestTabsViewModel(IEnumerable&lt;IMainScreenTabItem&gt; tabs)
      {
          Items.Add(new ViewTabModel("Foo1"));
          Items.Add(new ViewTabModel("Foo2"));
          Items.AddRange(tabs);
      }
  }
  
  public sealed class ViewTabModel : Screen, IMainScreenTabItem
  {
      public ViewTabModel(string displayName)
      {
          DisplayName = displayName;
      }
  }
  </code></pre>
  
  <p>And here is the view MainViewTestTabsView:</p>
  
  <pre><code>&lt;UserControl  x:Class="TestWpfApp.Views.MainViewTestTabsView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:TestWpfApp.Views"
      xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
      xmlns:viewModels="clr-namespace:TestWpfApp.ViewModels"
      xmlns:cal="http://www.caliburnproject.org"
      mc:Ignorable="d" Width="500" Height="500"&gt;
  &lt;Grid&gt;
      &lt;TabControl Name="Items"&gt;
          &lt;TabControl.ContentTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel&gt;
                      &lt;Label  cal:Bind.Model="{Binding}" x:Name="DisplayName" Height="200" Width="200" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/TabControl.ContentTemplate&gt;
      &lt;/TabControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>What I want to achieve - is to have TabControl with many tabs. Each tab has the same "view" (declared in DataTemplate) but to bind this view I want to use different viewModels (to be specific - the same model class [ViewTabModel ] but with different data)</p>
  
  <p>The size of tabs is to be declared at runtime as well as the data, which should be in the ViewTabModel model.</p>
  
  <p>In example below - I have two tabs, but changing them is not changing the Label (i have all the Time: "Foo1" Label, even if I click "Foo2" tab)</p>
  
  <p>I use caliburn.micro as a framework - with autofac bootstrap (if it matters)
  And I use propertyChanged.Fody (<a href="https://github.com/Fody/PropertyChanged" rel="nofollow noreferrer">https://github.com/Fody/PropertyChanged</a>) to omit all the propertychanged stuff in viewmodels.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>=== UPDATE ===</p>
  
  <p>Attaching minimal reproduction solution:</p>
  
  <p><a href="https://wetransfer.com/downloads/0b909bfd31a588dda99655f366eddad420170801192103/1d094a" rel="nofollow noreferrer">https://wetransfer.com/downloads/0b909bfd31a588dda99655f366eddad420170801192103/1d094a</a></p>
  
  <p>Plese, help! :)</p>
  
  <p>=== UPDATE 2 === </p>
  
  <p>Is anything unclear about my question ?:) Still no comments, no anwsers event with bounty on it.</p>
  
  <p>=== UPDATE 3 ===</p>
  
  <p>I've already posted COMPLETE view page (xaml) and COMPLETE model code (this is only this)</p>
  
  <p>I'm posting also AppBoostraper.cs and AppWindowManager.cs (but i suppose it is irrelevat here)</p>
  
  <p>AppBoostrapper.cs</p>
  
  <pre><code>using Autofac;
  using TestWpfApp.ViewModels;
  
  namespace TestWpfApp {
      using System;
      using System.Collections.Generic;
      using Caliburn.Micro;
  
      public class AppBootstrapper : CaliburnMetroAutofacBootstrapper&lt;MainViewTestTabsViewModel&gt;
      {
          protected override void ConfigureContainer(ContainerBuilder builder)
          {
              builder.RegisterType&lt;AppWindowManager&gt;().As&lt;IWindowManager&gt;().SingleInstance();
              var assembly = typeof(ShellViewModel).Assembly;
              builder.RegisterAssemblyTypes(assembly)
                  .Where(item =&gt; item.Name.EndsWith("ViewModel") &amp;&amp; item.IsAbstract == false)
                  .AsSelf()
                  .SingleInstance();
          }
      }
  }
  </code></pre>
  
  <p>It is inheriting CaliburnMetroAutofacContainer (<a href="https://github.com/ziyasal/Caliburn.Metro" rel="nofollow noreferrer">https://github.com/ziyasal/Caliburn.Metro</a>)</p>
  
  <p>AppWindowsManager.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Caliburn.Metro.Core;
  using MahApps.Metro.Controls;
  
  namespace TestWpfApp
  {
      public class AppWindowManager : MetroWindowManager
      {
          public override MetroWindow CreateCustomWindow(object view, bool windowIsView)
          {
              if (windowIsView)
              {
                  return view as ShellView;
              }
  
              return new ShellView
              {
                  Content = view
              };
          }
      }
  }
  </code></pre>
  
  <p>=== UPDATE 4 ===
  Apprently, changing control from:</p>
  
  <blockquote>
    <p>cal:Bind.Model="{Binding}" x:Name="DisplayName"</p>
  </blockquote>
  
  <p>to:</p>
  
  <blockquote>
    <p>Content="{Binding DisplayName}"</p>
  </blockquote>
  
  <p>Did the work. Although i'm not quite sure why?</p>
  
  <p>Now i want to do exactly the same. Only this time i want my view to be binded. So ViewModel is exactly the same. But this time:</p>
  
  <p>MainViewTestTabsView</p>
  
  <pre><code>&lt;UserControl  x:Class="TestWpfApp.Views.MainViewTestTabsView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:TestWpfApp.Views"
      xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
      xmlns:viewModels="clr-namespace:TestWpfApp.ViewModels"
      xmlns:cal="http://www.caliburnproject.org"
      mc:Ignorable="d" Width="500" Height="500"&gt;
  &lt;Grid&gt;
      &lt;TabControl Name="Items"&gt;
          &lt;TabControl.ContentTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel&gt;
                      &lt;local:ViewTab cal:Bind.Model="{Binding}" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/TabControl.ContentTemplate&gt;
      &lt;/TabControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>and ViewTab view is :</p>
  
  <pre><code>&lt;UserControl  x:Class="TestWpfApp.Views.ViewTab"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:TestWpfApp.Views"
      xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
      xmlns:viewModels="clr-namespace:TestWpfApp.ViewModels"
      xmlns:cal="http://www.caliburnproject.org"
      mc:Ignorable="d" Width="300" Height="300"&gt;
  &lt;Grid&gt;
      &lt;StackPanel&gt;
          &lt;Label x:Name="DisplayName"&gt;&lt;/Label&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>=== Update 5 => Happy final ===
  I've been sugested I should stick to the ViewModel first convention (as i declared i'm using) and my attempts was somhow view first. So i've changed it to:</p>
  
  <pre><code>&lt;ContentControl cal:View.Model="{Binding ActiveItem}" /&gt;
  </code></pre>
  
  <p>But nothing is rendered then</p>
  
  <p>If i declare it like this :</p>
  
  <pre><code>&lt;ContentControl cal:View.Model="{Binding}" /&gt;
  </code></pre>
  
  <p>There is only message saying : "Cannot find view for : [my_namspece].ViewTabModel
  It was weird and get me thinking. Maybe i'm not sticking to the convention. And it was true... </p>
  
  <p>My model was called:</p>
  
  <blockquote>
    <p>ViewTabModel </p>
  </blockquote>
  
  <p>Whereas it should be :</p>
  
  <blockquote>
    <p>ViewTabViewModel </p>
  </blockquote>
  
  <p>exactly the same thing with view. It should be called:</p>
  
  <blockquote>
    <p>ViewTabView.xaml</p>
  </blockquote>
  
  <p>After that, such construction:</p>
  
  <pre><code>&lt;ContentControl cal:View.Model="{Binding}" /&gt;
  </code></pre>
  
  <p>Is working correctly!! Thank you arcticwhite and grek40 for leading me to this solution</p>