title: Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception
body: <p>I'm having a problem with my WPF application, and i have no idea why. I'm far from an expert with XAML, and I don't understand this error, or even what part of the code is throwing the error.</p>
  
  <p>I have the following view:</p>
  
  <pre><code>&lt;DataGrid BorderThickness="0" Width="Auto" AutoGenerateColumns="False" AlternationCount="2"
            IsSynchronizedWithCurrentItem="True" AutomationProperties.AutomationId="PositionSummaryGrid" 
            ItemsSource="{Binding Path=BOEList}" RowDetailsVisibilityMode="VisibleWhenSelected"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="BOE Reference" Binding="{Binding Path=Reference}"/&gt;
          &lt;DataGridTextColumn Header="Account No" Binding="{Binding Path=AccountNo}"/&gt;
          &lt;DataGridTextColumn Header="LBL Invoice No" Binding="{Binding Path=InvoiceNo}"/&gt;
          &lt;DataGridTextColumn Header="Date Raised" Binding="{Binding Path=DateRaised}"/&gt;
          &lt;DataGridTextColumn Header="Value" Binding="{Binding Converter={StaticResource currencyConverter}, Path=Value}" Width="85"/&gt;
          &lt;DataGridTextColumn Header="Bank Charges" Binding="{Binding Converter={StaticResource currencyConverter}, Path=BankCharges}" Width="85"/&gt;
          &lt;DataGridTextColumn Header="Payment Due" Binding="{Binding Converter={StaticResource currencyConverter}, Path=PaymentDue}" Width="85"/&gt;
          &lt;DataGridTextColumn Header="Description" Binding="{Binding Path=Description}" Width="auto"/&gt;
      &lt;/DataGrid.Columns&gt;
      &lt;DataGrid.RowDetailsTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Payments}"&gt;
                  &lt;DataGridTextColumn Header="Payment Date" Binding="{Binding PaymentDate}"/&gt;
                  &lt;DataGridTextColumn Header="Payment Amount" Binding="{Binding Converter={StaticResource currencyConverter}, Path=PaymentAmount}"/&gt;
              &lt;/DataGrid&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGrid.RowDetailsTemplate&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Which is styled using the following:</p>
  
  <pre><code>&lt;Style x:Key="DataGridScrollViewer" TargetType="{x:Type ScrollViewer}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ScrollViewer}"&gt;
                  &lt;Grid Background="{TemplateBinding Background}"&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="*"/&gt;
                          &lt;ColumnDefinition Width="Auto"/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height="*"/&gt;
                          &lt;RowDefinition Height="Auto"/&gt;
                      &lt;/Grid.RowDefinitions&gt;
  
                      &lt;DockPanel Margin="{TemplateBinding Padding}"&gt;
                          &lt;ScrollViewer DockPanel.Dock="Top"
                                        HorizontalScrollBarVisibility="Hidden"
                                        VerticalScrollBarVisibility="Hidden"
                                        Focusable="false"&gt;
                              &lt;Border CornerRadius="4" Background="{StaticResource DefaultedBorderBrush}" Padding="4" Margin="0,4,0,4"&gt;
                                  &lt;DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"/&gt;
                              &lt;/Border&gt;
                          &lt;/ScrollViewer&gt;
  
                          &lt;ScrollContentPresenter Name="PART_ScrollContentPresenter" KeyboardNavigation.DirectionalNavigation="Local"/&gt;
                      &lt;/DockPanel&gt;
  
                      &lt;ScrollBar Name="PART_HorizontalScrollBar"
                          Orientation="Horizontal"
                          Grid.Row="1"
                          Maximum="{TemplateBinding ScrollableWidth}"
                          ViewportSize="{TemplateBinding ViewportWidth}"
                          Value="{TemplateBinding HorizontalOffset}"
                          Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/&gt;
  
                      &lt;ScrollBar Name="PART_VerticalScrollBar"
                          Grid.Column="1"
                          Maximum="{TemplateBinding ScrollableHeight}"
                          ViewportSize="{TemplateBinding ViewportHeight}"
                          Value="{TemplateBinding VerticalOffset}"
                          Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="DataGridColumnHeaderGripper" TargetType="{x:Type Thumb}"&gt;
      &lt;Setter Property="Width" Value="18"/&gt;
      &lt;Setter Property="Background" Value="#404040"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Thumb}"&gt;
                  &lt;Border Padding="{TemplateBinding Padding}" Background="Transparent"&gt;
                      &lt;Rectangle HorizontalAlignment="Center" Width="1" Fill="{TemplateBinding Background}"/&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="{x:Type DataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}"&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
      &lt;Setter Property="Foreground" Value="#ffffff"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridColumnHeader}"&gt;
                  &lt;Grid&gt;
                      &lt;Border Name="HeaderBorder" BorderThickness="0,1,0,1" BorderBrush="{x:Null}" Background="{x:Null}" Padding="2,0,2,0"&gt;
                          &lt;ContentPresenter Name="HeaderContent" Margin="0,0,0,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                      &lt;/Border&gt;
                      &lt;Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0" Style="{StaticResource DataGridColumnHeaderGripper}" Foreground="#FFFFFFFF" Background="{x:Null}"/&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                          &lt;Setter Property="Background" TargetName="PART_HeaderGripper" Value="{x:Null}"/&gt;
                          &lt;Setter Property="Foreground" TargetName="PART_HeaderGripper" Value="#FFFCFCFC"/&gt;
                          &lt;Setter Property="Background" TargetName="HeaderBorder" Value="{x:Null}"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsPressed" Value="true"&gt;
                          &lt;Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/&gt;
                          &lt;Setter Property="Foreground" TargetName="PART_HeaderGripper" Value="#FFE0E0E0"/&gt;
                          &lt;Setter Property="Background" TargetName="PART_HeaderGripper" Value="{x:Null}"/&gt;
                          &lt;Setter Property="Background" TargetName="HeaderBorder" Value="{x:Null}"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                          &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/&gt;
                          &lt;Setter Property="Background" TargetName="PART_HeaderGripper" Value="{x:Null}"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}"&gt;
      &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
      &lt;Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/&gt;
      &lt;Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/&gt;
      &lt;Setter Property="ScrollViewer.CanContentScroll" Value="true"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
      &lt;Setter Property="GridLinesVisibility" Value="None"/&gt;
      &lt;Setter Property="SelectionMode" Value="Extended"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGrid}"&gt;
                  &lt;Border Name="Border" BorderThickness="1" BorderBrush="{x:Null}" Background="{x:Null}"&gt;
                      &lt;ScrollViewer Style="{DynamicResource DataGridScrollViewer}" Foreground="#FFFFFFFF"&gt;
                          &lt;ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                      &lt;/ScrollViewer&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsGrouping" Value="true"&gt;
                          &lt;Setter Property="ScrollViewer.CanContentScroll" Value="false"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                          &lt;Setter TargetName="Border" Property="Background" Value="#AAAAAA"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="{x:Type DataGridRow}" TargetType="{x:Type DataGridRow}"&gt;
      &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Center" /&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center" /&gt;
      &lt;Setter Property="Foreground" Value="#000000"/&gt;
      &lt;Setter Property="DetailsVisibility" Value="Collapsed"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridRow}"&gt;
                  &lt;Border Name="Border" Padding="2" SnapsToDevicePixels="true" CornerRadius="4" Margin="0,4,0,0"&gt;
                      &lt;SelectiveScrollingGrid&gt;
                          &lt;Grid.ColumnDefinitions&gt;
                              &lt;ColumnDefinition Width="Auto"/&gt;
                              &lt;ColumnDefinition Width="*"/&gt;
                          &lt;/Grid.ColumnDefinitions&gt;
  
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="*"/&gt;
                              &lt;RowDefinition Height="Auto"/&gt;
                          &lt;/Grid.RowDefinitions&gt;
  
                          &lt;DataGridCellsPresenter Grid.Column="1"
                                           ItemsPanel="{TemplateBinding ItemsPanel}"
                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
  
                          &lt;DataGridDetailsPresenter  SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=AreRowDetailsFrozen, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}}"
                                              Grid.Column="1" Grid.Row="1"
                                              Visibility="{TemplateBinding DetailsVisibility}" /&gt;
  
                          &lt;DataGridRowHeader SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"  Grid.RowSpan="2"
                                      Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Row}}"/&gt;
                      &lt;/SelectiveScrollingGrid&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="ItemsControl.AlternationIndex" Value="0"&gt;
                          &lt;Setter Property="Background" TargetName="Border"  Value="#A5FFFFFF"&gt;&lt;/Setter&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="ItemsControl.AlternationIndex" Value="1"&gt;
                          &lt;Setter Property="Background" TargetName="Border"  Value="#01FFFFFF"&gt;&lt;/Setter&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsSelected" Value="true"&gt;
                          &lt;Setter Property="Background" TargetName="Border" Value="#99B4C6"/&gt;
                          &lt;Setter Property="Foreground" Value="#000000"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                          &lt;Setter Property="Background" TargetName="Border" Value="#c5d7e5"/&gt;
                          &lt;Setter Property="Foreground" Value="#000000"/&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                          &lt;Setter Property="Foreground" Value="#000000"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;              
  &lt;/Style&gt;
  
  &lt;Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt;
                  &lt;Grid&gt;
                      &lt;Rectangle Fill="White" Opacity="0"&gt;&lt;/Rectangle&gt;
                      &lt;ContentPresenter HorizontalAlignment="Center"/&gt; 
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>The ViewModel is:</p>
  
  <pre><code>public class BoeListViewModel : Screen
  {
      #region Fields
      private BOEManager _Manager;
      private ObservableCollection&lt;BOE&gt; _boeList;
      #endregion
  
      #region Properties
      public ObservableCollection&lt;BOE&gt; BOEList
      {
          get { return _boeList; }
          set
          {
              _boeList = value;
              NotifyOfPropertyChange(() =&gt; BOEList);
          }
      }
  
      public string Header
      {
          get { return "CURRENT BOES"; }
      }
      #endregion
  
      #region Constructor
      public BoeListViewModel()
      {
          this.DisplayName = "Current BOEs";
          _Manager = IoC.Get&lt;BOEManager&gt;();
          BOEList = _Manager.load();
      }
      #endregion
  </code></pre>
  
  <p>The BOE Model is a simple collection of properties including:</p>
  
  <pre><code>public ObservableCollection&lt;Payment&gt; Payments
  {
      get { return _payments; }
      set
      {
          if (value != _payments)
          {
              _payments = value;
              NotifyOfPropertyChange(() =&gt; Payments);
          }
      }
  }
  </code></pre>
  
  <p>With payment again a simple collection of Properties (this is just prototyping).</p>
  
  <p>Exception appears to happen inside Caliburn Micro's <code>OnStartup()</code> method, but I'm not 100% sure about that. and it is:</p>
  
  <pre><code>  System.Windows.Markup.XamlParseException occurred
    HResult=-2146233087
    Message=Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.
    Source=PresentationFramework
    LineNumber=0
    LinePosition=0
    StackTrace:
         at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
         at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
         at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
         at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
         at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
         at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
         at System.Windows.FrameworkElement.ApplyTemplate()
         at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
         at System.Windows.UIElement.Measure(Size availableSize)
         at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
         at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean&amp; hasDesiredSizeUChanged)
         at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
         at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
         at System.Windows.UIElement.Measure(Size availableSize)
         at System.Windows.ContextLayoutManager.UpdateLayout()
         at System.Windows.UIElement.UpdateLayout()
         at System.Windows.Interop.HwndSource.SetLayoutSize()
         at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
         at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
         at System.Windows.Window.SetRootVisual()
         at System.Windows.Window.SetRootVisualAndUpdateSTC()
         at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
         at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
         at System.Windows.Window.CreateSourceWindowDuringShow()
         at System.Windows.Window.SafeCreateWindowDuringShow()
         at System.Windows.Window.ShowHelper(Object booleanBox)
         at System.Windows.Window.Show()
         at Caliburn.Micro.WindowManager.ShowWindow(Object rootModel, Object context, IDictionary`2 settings) in c:\Users\Rob\Documents\CodePlex\caliburnmicro\src\Caliburn.Micro.WPF\WindowManager.cs:line 75
    InnerException: System.InvalidOperationException
         HResult=-2146233079
         Message=Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
         Source=PresentationFramework
         StackTrace:
              at System.Windows.Controls.ItemCollection.CheckIsUsingInnerView()
              at System.Windows.Controls.ItemCollection.Add(Object newItem)
              at System.Xaml.Schema.XamlTypeInvoker.AddToCollection(Object instance, Object item)
              at MS.Internal.Xaml.Runtime.ClrObjectRuntime.Add(Object collection, XamlType collectionType, Object value, XamlType valueXamlType)
         InnerException: 
  </code></pre>
  
  <p>sorry for the code dump, but I have no real clue what part of this code is causing the error, as it just throws the error without a location, I'm fairly certain is has to do with the <code>RowDetails</code>, as without the <code>RowDetails</code> this works without an issue, however I cannot for the life of me work out what is going on.</p>
  
  <p>Also, if someone could let me know how to track down these kind of error?</p>
  
  <p>EDIT:</p>
  
  <p>Two things. </p>
  
  <p>Removing the line <code>RowDetailsVisibilityMode="VisibleWhenSelected"</code> allows the application to run without an exception, however obviously then the details row is never shown.</p>
  
  <p>Secondly, commenting out the <code>DataGridDetailsPresenter</code> inside the DataGrid row style also has the same effect of allowing this to run, so it must be something with that, however that code is taken straight from Microsoft examples.</p>