title: Dynamically use different UserControl based on datacontext
body: <p>I have a chunk of xaml that duplicates the same pattern six times and would like to reduce that footprint by eliminating the duplication.  I'm running into one snag that I need help with.</p>
  
  <p>Background:  I have a class that instantiates another class six times (phases of a project as you will).  </p>
  
  <pre><code>   public ECN(string ecnNumber) {
          _ECNNumber = ecnNumber;
  
          //instantiate each phase to be populated or not
          _PurchaseParts = new ECNPhase();
          _PieceParts = new ECNPhase();
          _Weldments = new ECNPhase();
          _BOMCfg = new ECNPhase();
          _Cleanup = new ECNPhase();
          _PRAF = new ECNPhase();   
      }
  </code></pre>
  
  <p>Inside each phase is a collection of Changes (another class) referenced in the ECNPhase Class.  Each phase has data that is unique to each phase that is shown in a unique view, this is where my snag is which I will show later.</p>
  
  <p>Example of the duplicate xaml Code with the main difference being the different view inside each expander:</p>
  
  <pre><code>&lt;StackPanel Margin="0"&gt;
  
      &lt;!--Section for Purchase parts--&gt;
      &lt;StackPanel Orientation="Horizontal" &gt;
          &lt;CheckBox Margin="0,5,5,5" IsChecked="{Binding Path=MyWorkspace.CurrentSelectedItem.PurchaseParts.HasPhase,Mode=TwoWay}"/&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;StackPanel.Style&gt;
                  &lt;Style TargetType="{x:Type StackPanel}"&gt;
                      &lt;Setter Property="IsEnabled" Value="False"/&gt;
                      &lt;Style.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding Path=MyWorkspace.CurrentSelectedItem.PurchaseParts.HasPhase}" Value="True"&gt;
                              &lt;Setter Property="IsEnabled" Value="True"/&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/StackPanel.Style&gt;
              &lt;Expander Header="Purchase Parts" Margin="0,0,10,0" Width="110"&gt;
                  &lt;view:PurchasePartsView/&gt;
              &lt;/Expander&gt;
              &lt;CheckBox Content="Submit" Margin="10,5,0,5"/&gt; &lt;!--add a command to handle the submit checkbox event--&gt;
              &lt;Label Content="Status:" Margin="10,0,0,0" HorizontalContentAlignment="Right" Width="60"/&gt;
              &lt;Label Content="{Binding Path=MyWorkspace.CurrentSelectedItem.PurchaseParts.Status}"/&gt;
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
  
      &lt;!--Section for Piece Parts--&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;CheckBox  Margin="0,5,5,5" IsChecked="{Binding Path=MyWorkspace.CurrentSelectedItem.PieceParts.HasPhase,Mode=TwoWay}"/&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;StackPanel.Style&gt;
                  &lt;Style TargetType="{x:Type StackPanel}"&gt;
                      &lt;Setter Property="IsEnabled" Value="False"/&gt;
                      &lt;Style.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding Path=MyWorkspace.CurrentSelectedItem.PieceParts.HasPhase}" Value="True"&gt;
                              &lt;Setter Property="IsEnabled" Value="True"/&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/StackPanel.Style&gt;
              &lt;Expander Header="Piece Parts"  Margin="0,0,10,0" Width="110"&gt;
                  &lt;view:PiecePartsView/&gt;
              &lt;/Expander&gt;
              &lt;CheckBox Content="Submit" Margin="10,5,0,5"/&gt; &lt;!--add a command to handle the submit checkbox event--&gt;
              &lt;Label Content="Status:" Margin="10,0,0,0" HorizontalContentAlignment="Right" Width="60"/&gt;
              &lt;Label Content="{Binding Path=MyWorkspace.CurrentSelectedItem.PieceParts.Status}"/&gt;
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
      &lt;!--duplicated four more times--&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>What I'd like to do is:</p>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;view:PhaseView DataContext="{Binding Path=MyWorkspace.CurrentSelectedItem.PurchaseParts}"/&gt;
      &lt;view:PhaseView DataContext="{Binding Path=MyWorkspace.CurrentSelectedItem.PieceParts}"/&gt;
      &lt;!--four more phases--&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>Where the PhaseView will be the template that handles the duplication and this is where I'm hitting a snag. Each phase needs a unique view (userControl) selected based off of the datacontext of the PhaseView.</p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal" &gt;
      &lt;CheckBox Margin="0,5,5,5" IsChecked="{Binding Path=HasPhase,Mode=TwoWay}"/&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;StackPanel.Style&gt;
              &lt;Style TargetType="{x:Type StackPanel}"&gt;
                  &lt;Setter Property="IsEnabled" Value="False"/&gt;
                  &lt;Style.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding Path=HasPhase}" Value="True"&gt;
                          &lt;Setter Property="IsEnabled" Value="True"/&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/StackPanel.Style&gt;
          &lt;Expander Header="DisplayName" Margin="0,0,10,0" Width="110"&gt;
              &lt;!--add somthing here to select the correct view based on the datacontext--&gt;
              &lt;!--&lt;local:PurchasePartsView/&gt;  This user control adds a datagrid that is unique to each phase--&gt;
          &lt;/Expander&gt;
          &lt;CheckBox Content="Submit" Margin="10,5,0,5"/&gt; &lt;!--add a command to handle the submit checkbox event--&gt;
          &lt;Label Content="Status:" Margin="10,0,0,0" HorizontalContentAlignment="Right" Width="60"/&gt;
          &lt;Label Content="{Binding Path=Status}"/&gt;
      &lt;/StackPanel&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>I was thinking of using a datatrigger somehow lik shown below, but I haven't had any luck figuring it out.  I know there's got to be a way to do this, and it's probably something simple and dumb.  Any help would be much appreciated.</p>
  
  <pre><code>&lt;DataTrigger Binding="{Binding Path=DisplayName}" Value="Purchase Parts"&gt;
     &lt;Setter Property="DataContext" Value="{Binding }"/&gt; &lt;!--Don't know how to bind the DataContext--&gt;
  &lt;/DataTrigger&gt;
  </code></pre>
  
  <p>Thanks,</p>