title: Why am I getting an "Items Collection Must Be Empty" exception with nested DataGrids?
body: <p>I'm using nested DataGrids to manage classes with nested class collections.</p>
  
  <p>Each nested class is represented by a cell template.</p>
  
  <p>This is a minimal mockup which reproduces the issue : </p>
  
  <pre><code>using System.ComponentModel;
  using System.Linq;
  
  namespace ICMBE{
      public class Outer : INotifyPropertyChanged {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private Nested1[ ] _categories = Enumerable.Range( 1, 1
                      ).Select( n =&gt; new Nested1( ) ).ToArray( );
  
          public Nested1[ ] Categories {
              get { return this._categories; }
          }
      }
  
      public class Nested1 : INotifyPropertyChanged {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private Nested2[ ] _questions = Enumerable.Range( 1, 5
                      ).Select( n =&gt; new Nested2( ) ).ToArray( );
  
          public Nested2[ ] Questions {
              get { return this._questions; }
          }
      }
  
      public class Nested2 : INotifyPropertyChanged {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private Nested3[] _answers = Enumerable.Range( 1, 5
                  ).Select( n =&gt; new Nested3( ) ).ToArray( );
  
          public Nested3[ ] Answers {
              get { return this._answers; }
          }
      }
  
      public class Nested3 : INotifyPropertyChanged {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private string _foo = "Bar";
  
          public string Foo { get { return this._foo; } }
      }
  }
  </code></pre>
  
  <p>Create a field value and expose it in the application : </p>
  
  <pre><code>using System.ComponentModel;
  
  namespace IMCBE{
      public partial class App : INotifyPropertyChanged {
          public event PropertyChangedEventHandler PropertyChanged;
          private Outer _outer = new Outer( );
      }
  }
  
  namespace IMCBE{
  public partial class Program {
          /// &lt;summary&gt;
          /// Bla bla bla
          /// &lt;/summary&gt;
          public Outer Outer{
              get { return this._outer; }
          }
      }
  }
  </code></pre>
  
  <p>Using this window - </p>
  
  <pre><code>&lt;Window
      x:Class="ICMBE.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:Controls="clr-namespace:WPFTools.Controls;assembly=WPFTools"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:WhyAreMyRowNumbersChanging"
      mc:Ignorable="d"
      Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;!-- Answers --&gt;
          &lt;DataTemplate x:Key="dtAnswerTemplate"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="100" /&gt;
                      &lt;ColumnDefinition Width="5" /&gt;
                      &lt;ColumnDefinition /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;ToggleButton&gt;
                      &lt;Viewbox&gt;
                          &lt;TextBlock Text="Correct" /&gt;
                      &lt;/Viewbox&gt;
                  &lt;/ToggleButton&gt;
                  &lt;TextBox Text="Bar" /&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
  
          &lt;!-- Questions --&gt;
          &lt;DataTemplate x:Key="dtQuestionTemplate"&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="25" /&gt;
                      &lt;RowDefinition Height="5" /&gt;
                      &lt;RowDefinition Height="25" /&gt;
                      &lt;RowDefinition Height="5" /&gt;
                      &lt;RowDefinition Height="50" /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="128" /&gt;
                      &lt;ColumnDefinition Width="5" /&gt;
                      &lt;ColumnDefinition Width="128" /&gt;
                      &lt;ColumnDefinition Width="5" /&gt;
                      &lt;ColumnDefinition /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Viewbox Grid.ColumnSpan="3"&gt;
                      &lt;TextBlock Text="Question" /&gt;
                  &lt;/Viewbox&gt;
                  &lt;TextBox Grid.Row="2" Grid.ColumnSpan="3" Text="Foo" /&gt;
                  &lt;ToggleButton Grid.Row="4"&gt;
                      &lt;Viewbox&gt;
                          &lt;TextBlock Text="Bonus" /&gt;
                      &lt;/Viewbox&gt;
                  &lt;/ToggleButton&gt;
                  &lt;Button Grid.Row="4" Grid.Column="2" /&gt;
                  &lt;Viewbox Grid.Column="4"&gt;
                      &lt;TextBlock Text="Answers" /&gt;
                  &lt;/Viewbox&gt;
                  &lt;DataGrid
                      AutoGenerateColumns="False" CanUserAddRows="False"
                      CanUserDeleteRows="False" Grid.Column="4"
                      Grid.RowSpan="4" ItemsSource="{Binding Nested}"&gt;
                      &lt;DataGridTemplateColumn
                          Width="*"
                          CellTemplate="{StaticResource dtAnswerTemplate}" /&gt;
                  &lt;/DataGrid&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
  
          &lt;!-- Categories --&gt;
          &lt;DataTemplate x:Key="dtCategoryTemplate"&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="25" /&gt;
                      &lt;RowDefinition /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="256" /&gt;
                      &lt;ColumnDefinition Width="5" /&gt;
                      &lt;ColumnDefinition /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Viewbox&gt;
                      &lt;TextBlock Text="Outer"/&gt;
                  &lt;/Viewbox&gt;
                  &lt;DataGrid
                      AutoGenerateColumns="False" CanUserAddRows="False"
                      CanUserDeleteRows="False" Grid.Row="2"
                      Grid.ColumnSpan="3" HeadersVisibility="None"
                      ItemsSource="{Binding Nested}"&gt;
                      &lt;DataGrid.Columns&gt;
                          &lt;DataGridTemplateColumn
                              Width="*"
                              CellTemplate="{StaticResource dtQuestionTemplate}" /&gt;
                      &lt;/DataGrid.Columns&gt;
                  &lt;/DataGrid&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;DataGrid
          Grid.Row="1"
          ItemsSource="{Binding Outer.Nested, Source={x:Static Application.Current}}"
          MaxHeight="Infinity" ScrollViewer.CanContentScroll="True"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTemplateColumn
                  Width="*" CellTemplate="{StaticResource dtCategoryTemplate}" /&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Running this causes the program to just crash. The exception is "Items collection must be empty".</p>
  
  <p>From what I've read, this happens if you set the <code>Items</code> and <code>ItemsSource</code> are both set, but this isn't happening here.</p>
  
  <p>Why is this crash occurring?</p>