title: WPF Create user control dynamically on change of observablecollection OR Visualize nested ObservableCollections by using nested User controls
body: <p>I am new to WPF, and I can't find a solution to my problem.</p>
  
  <p>I've got an Observable Collection in my ViewModel:</p>
  
  <pre><code>ObservableCollection&lt;Process&gt;
  </code></pre>
  
  <p>Within the Process class is another ObervableCollection:</p>
  
  <pre><code>ObservableCollection&lt;BurstTime&gt;
  </code></pre>
  
  <p>I want to add a new User control (what can vizualize the actual state of Process data, and it's burst times) dynamically to one of my StackPanel in my view each time, when a new Process is activated, and remove it when the process is ended (when the observable collection changes).. During it's run, I want to diplay it's Burst Time collection, what can change in time..</p>
  
  <p>I've tried to subscribe to the CollectionChanged event in my user control's codebehind and use a User Control inside another User Control and dynamicalli create required TextBlocks when my event handler runs, but I always get a System.Reflection.TargetInvocationException (inner exception: System.NullReferenceException) when my outer InitializeComponent() is running..</p>
  
  <pre><code>public ResourceUseView()
  {
      InitializeComponent();
      ((ObservableCollection&lt;Process&gt;)DataContext).CollectionChanged += ResourceUseView_CollectionChanged;
  }
  </code></pre>
  
  <p>Is it possible to do it? Is it possible to create or remove User controls in my outer UserControl's codebehind while reacting to ObservableCollection's element changes, and update the outer when an element changes in the inner ObservableCollection? Are there other ways than instantiate a UserControl inside another User Control dynamically? Is there any special control, what can display ObservableCollection in ObservableCollection?</p>
  
  <p>(If it'is possible, I would like to have different user controls as much as elements my ObservableCollection have..)</p>
  
  <p>Thanks for your help!</p>