title: WPF Dynamically add single control through binding using MVVM
body: <p>Okay, so I did a bunch of searching around for this and I either can't seem to wrap my head around the right question, or it just isn't done...</p>
  
  <p>Basically I have a modular application which contains various data-driven 'Applets', which can potentially change and are dynamically added in based upon the permissions that have been provided to the user.  I've already built this application in Flex, and we are moving it to WPF through MVVM because it's a million times easier for us to code and maintain.</p>
  
  <p>Now, there are two issues I'm trying to figure out:</p>
  
  <p><strong>Issue #1</strong></p>
  
  <p>I need to be able to dynamically add a single control through binding.  I've already built bindings using data templates on ItemsControls that hook up to a list of objects in the ViewModel, and that works wonderfully, but that system seems a little hacky when I know that there will only ever be ONE control but it gets me by for now.</p>
  
  <p>Here is my code for that to show why this seems hacky to me:</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding ApplicationList}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;CTRL:PortalApplicationControl /&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>/// &lt;summary&gt;
  /// A List to contain the currently active application for binding to the ItemsControl in XAML
  /// &lt;/summary&gt;
  public List&lt;Models.Application&gt; ApplicationList
  {
      get { return (List&lt;Models.Application&gt;)GetValue(ApplicationListProperty); }
      set { SetValue(ApplicationListProperty, value); }
  }
  public static readonly DependencyProperty ApplicationListProperty =
      DependencyProperty.Register("ApplicationList", typeof(List&lt;Models.Application&gt;), typeof(ApplicationVM));
  
  /// &lt;summary&gt;
  /// The currently active application
  /// &lt;/summary&gt;
  public Models.Application Application
  {
      get { return (Models.Application)GetValue(ApplicationProperty); }
      set { SetValue(ApplicationProperty, value); ApplicationList = new List&lt;Models.Application&gt;() { Application }; }
  }
  public static readonly DependencyProperty ApplicationProperty =
      DependencyProperty.Register("Application", typeof(Models.Application), typeof(ApplicationVM));
  </code></pre>
  
  <p>As you can see, it's hacky in the sense that the objects which are binding to properties on the Application dependency property are able to call '{Binding Application.Name}' but the ItemsControl has to call '{Binding ApplicationList}' and the setter for Application has to instantiate a new 'List' of length 1 to set the ApplicationList object in turn....it works, but it's not very elegant and I want to know if there is another way of accomplishing this task</p>
  
  <p><strong>Issue #2</strong></p>
  
  <p>Okay, now we get to the part that throws a monkey wrench in the entire structure above:  What if I want different controls in the data template based upon which Application object is currently active?  I.E. If Application1 is currently active, use Application1View, but if Application2 is currently active, use Application2View...This seems like I am going to have to do some work in the code behind to get it to work...and that strikes me as a big no no if I'm sticking with the MVVM structure</p>
  
  <p>Sorry for being long winded, I hope that the question makes sense and thanks for any help you guys can offer...this is putting my project at a standstill until I can resolve the above issues</p>