title: WPF DataTemplate / DataTemplateSelector -- Best approach for a ViewModel used by 2 different Views?
body: <p>Basically, I have the following scenario:</p>
  
  <p>ViewModel: <code>FooViewModel : BaseViewModel</code>, <code>BarViewModel : BaseViewModel</code><br />
  Views: <code>MainView</code>, <code>FooView</code>, <code>BarView</code><br />
  <br />
  Right now I "inject" the view and set the <code>DataContext</code> using <code>DataTemplate</code> and <code>DataTemplateSelector</code>. Obviously, my <code>ItemsControl</code> <code>ItemSource</code> is bound to an <code>ObservableCollection&lt;BaseViewModel&gt;</code> in which it contains (for now) an instance of a <code>FooViewModel</code> and a <code>BarViewModel</code></p>
  
  <p>The problem is I want to introduce a <code>AlternateFooView</code> which I want to utilize the same <code>FooViewModel</code>. I figure I'll create another <code>DataTemplate</code> and have my <code>DataTemplateSelector</code> return it, but there needs to be special logic to determine which DataTemplate to return (I can't just go by which ViewModel is there), and that means I'll have to have some type of property or field in the BaseViewModel. I don't know if that's really a good idea because that seems to be introducing a field/property into the ViewModel that is only used to select a view. It won't hurt my unit testing, but it seems like a waste to include a field just to help decide which UI View to choose. I don't think it breaks MVVM, but I'm curious if anyone out there has any other better ideas? The alternative ideas I had I dislike even more...</p>
  
  <p>Idea #2:<br />
  - Turn FooViewModel into a base class that 2 different FooViewModel's extend (i.e. BaseFooViewModel, FooViewModel, DifferentFooViewModel). This seems stupid because there really isn't any difference between FooViewModel and DifferentFooViewModel aside from their class type.</p>
  
  <p>Idea #3:<br />
  - Just copy FooViewModel and make it FooViewModel2 (it'll be exactly identical to FooViewModel). This seems even worse than idea #2.</p>
  
  <p><hr>
  Sample-Code (Adjusted obviously):</p>
  
  <pre><code>public abstract class BaseViewModel : NotificationObject
  {
      //Common Stuff
  }
  
  public abstract MainViewModel : NotificationObject
  {
      public MainViewModel()
      {
          MyItems = new ObservableCollection&lt;BaseViewModel&gt;()
          {
              new FooViewModel();
              new BarViewModel();
              new FooViewModel(); //New Item -- I want it to use the DifferentFooView
          }
          //Load items from a DAL later
      }
  
      public ObservableCollection&lt;BaseViewModel&gt; MyItems { get; set; }
  
      //Other Stuff
  }
  
  &lt;l:MyItemsControl ItemSource={Binding MyItems} ContentTemplateSelector={StaticResource MyTemplateSelector} /&gt;
  </code></pre>
  
  <p><hr>
  Thanks!</p>