title: Changing the User Control of Window dynamically in WPF (MVVM)
body: <p>I am working on wpf mvvm pattern.
  I have a user control in which i am loading a list of checkboxes in DataGridCheckBoxColumn and binding it to IsSelected property from my viewmodel.</p>
  
  <p>Tha xaml code is like this:</p>
  
  <pre><code>&lt;DataGrid Width="150" Grid.Row="0" Background="LightGray" CanUserAddRows="False"  AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGridCustomers" ItemsSource="{Binding Path=UsecaseListItems}" CanUserResizeColumns="False" CanUserResizeRows="False"&gt;
              &lt;DataGrid.Columns&gt;
  
  
                      &lt;DataGridCheckBoxColumn Binding="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}"  Width="50"&gt;
  
                          &lt;DataGridCheckBoxColumn.HeaderTemplate&gt;
                              &lt;DataTemplate x:Name="dtAllChkBx"&gt;
                              &lt;CheckBox Name="cbxAll" FontWeight="Bold" Content="All" IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=UserControl },Mode=TwoWay}"/&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridCheckBoxColumn.HeaderTemplate&gt;
                      &lt;/DataGridCheckBoxColumn&gt;
  
                  &lt;DataGridTextColumn Width="85" Binding="{Binding Path=UsecaseName}" Header="UsecaseName"  IsReadOnly="True" &gt;
                      &lt;DataGridColumn.HeaderStyle&gt;
                          &lt;Style TargetType="DataGridColumnHeader"&gt;
                              &lt;Setter Property="FontWeight" Value="Bold"/&gt;
                              &lt;Setter Property="Foreground" Value="Black"&gt;&lt;/Setter&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridColumn.HeaderStyle&gt;
                  &lt;/DataGridTextColumn&gt;
  
  
  
              &lt;/DataGrid.Columns&gt;
            &lt;/DataGrid&gt;
  </code></pre>
  
  <p>My HomeViewModel is like this:</p>
  
  <pre><code>private bool _IsSelected;
          public bool IsSelected
          {
              get { return _IsSelected; }
              set
              {
                  _IsSelected = value;
                  OnPropertyChanged("IsSelected");
              }
          }
  
          private Control _someUserControl;
          public Control CCS01
          {
              get { return _someUserControl; }
              set { _someUserControl = value; }
          }
  
          private UserControl _content;
  
          internal void SetNewContent(UserControl _content)
          {
              ContentWindow = _content;
          }
  
          public UserControl ContentWindow
          {
              get { return _content; }
              set
              {
                  _content = value;
                  OnPropertyChanged("ContentWindow");
              }
          }
  </code></pre>
  
  <p>For my 1st checkbox (CCS01), I have created a view(CCS01.xaml) which contains a few labels and textboxes in grid format.And its corresponding ViewModel is below:</p>
  
  <pre><code>public class CCS01ViewModel: BaseNotifyPropertyChanged
      {
          HomeViewModel _homeViewModel;
          public ICommand OpenUsersCommand { get; private set; }
  
  
          public CCS01ViewModel(HomeViewModel mainModel)
          {
              this._homeViewModel = mainModel;
              //this._model = model;
              OpenUsersCommand = new RelayCommand(OpenUsers, CanOpenUsers);
          }
  
          private void OpenUsers(object _param)
          {
              //UsersPanelViewModel upmodel = new UsersPanelViewModel(_mainModel, _model);
              //UsersPanel up = new UsersPanel();
              //up.DataContext = upmodel;
              //_mainModel.SetNewContent(up);
          }
  
          private bool CanOpenUsers(object _param)
          {
              return true;
          }
  
  }
  </code></pre>
  
  <p>I want to load the selected checkbox view in the main UserControl(ExecutionDetails.xaml) . Currently, I am loading it with the first Checkbox view in it by default like this:</p>
  
  <pre><code>&lt;StackPanel&gt;
       &lt;Grid Name="HostGrid"&gt;
              &lt;ContentControl Content="{Binding ContentWindow}"/&gt;
       &lt;/Grid&gt;
  &lt;/StackPanel&gt; 
  </code></pre>
  
  <p>Please suggest me how to bind the view with respective checkboxes user controls dynamically based on the checkbox selection. </p>
  
  <p>The code behind of this(ExecutionDetails.xaml.cs) is like this:</p>
  
  <pre><code>public partial class ExecutionDetails : UserControl
      {
          public ExecutionDetails()
          {
              InitializeComponent();
  
              HomeViewModel viewmodel = new HomeViewModel();
              CCS01ViewModel ccViewModel = new CCS01ViewModel(viewmodel);
  
              CCS01 obj = new CCS01();
              obj.DataContext = ccViewModel;
              viewmodel.ContentWindow = obj;
              this.DataContext = viewmodel;
          }
      }
  </code></pre>