title: How to debug binding in WPF
body: <p>I have a VS2008, C# WPF, Excel AddIn; Under some case, My addin throws exception like </p>
  
  <pre><code>A first chance exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
  A first chance exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll
  </code></pre>
  
  <p>but I could not locate where the exception comes from. I know it is b/c data binding. but unable to find out where.  Each time I step in, VS tracks to a method which executes w/o error, then after that, the exception throws and but no clue of which line of code. </p>
  
  <p>I have been struggling this for days and could not make a little progress.
  Pls help out. thanks </p>
  
  <p>Edit, it is too long to fit in comment. So I just put xaml file here. @xmal file that throws the exception.  It is <code>DataGridComboBoxColumn</code> that throws the exception</p>
  
  <pre><code>&lt;UserControl x:Class="View.BasketView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" &gt;
      &lt;UserControl.Resources&gt;
          &lt;sharedC:FunctionToHiddenVisibility x:Key="enumSRToVis"/&gt;
          &lt;sharedC:FunctionToHiddenVisibility x:Key="enumCSToVis"/&gt;
          &lt;Style x:Key="DataGridRowStyle"  TargetType="{x:Type dg:DataGridRow}"&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="AlternationIndex" Value="1" &gt;
                      &lt;Setter Property="Background" Value="Beige" /&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
              &lt;Setter Property="AllowDrop" Value="True" /&gt;
              &lt;Setter Property="Margin" Value="0 2 0 2" /&gt;            
          &lt;/Style&gt;
          &lt;Style x:Key="DataGridStyle" TargetType="{x:Type dg:DataGrid}"&gt;
              &lt;Setter Property="AlternationCount" Value="2" /&gt;
              &lt;Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" /&gt;
          &lt;/Style&gt;
          &lt;Style TargetType="{x:Type MenuItem}"&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="MenuItem.IsHighlighted" Value="True" &gt;
                      &lt;Setter Property="BorderBrush" &gt;
                          &lt;Setter.Value&gt;
                              &lt;SolidColorBrush Color="Gray"&gt;&lt;/SolidColorBrush&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                      &lt;Setter Property="BorderThickness" Value="1"/&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;        
      &lt;/UserControl.Resources&gt;
      &lt;GroupBox&gt;
          &lt;GroupBox.Header&gt;
              &lt;TextBlock FontSize="14" FontFamily="Verdana" Text="{Binding Header,Mode=OneWay}"&gt;&lt;/TextBlock&gt;
          &lt;/GroupBox.Header&gt;
  
          &lt;Grid&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="33"&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height="*" &gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
  
              &lt;Border Margin="2 2 2 0"&gt;
                  &lt;Grid&gt;
  
                      &lt;Menu Background="Transparent"&gt;
                          &lt;Menu.Resources&gt;
                              &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /&gt;
                          &lt;/Menu.Resources&gt;
                          &lt;MenuItem IsEnabled="{Binding IsItemSelected}" Click="EditClick" ToolTip="Edit Relation(s)" Background="Transparent"&gt;
                              &lt;MenuItem.Header&gt;
                                  &lt;Image Width="16" Height="16" Source="{Binding EditImageFilePath}"/&gt;
                              &lt;/MenuItem.Header&gt;
                          &lt;/MenuItem&gt;
                          &lt;MenuItem IsEnabled="{Binding IsItemSelected}" Click="DeleteClick" ToolTip="Delete Relation(s)" Background="Transparent"&gt;
                              &lt;MenuItem.Header&gt;
                                  &lt;Image Width="16" Height="16" Source="{Binding DeleteImageFilePath}"/&gt;
                              &lt;/MenuItem.Header&gt;
                          &lt;/MenuItem&gt;
                      &lt;/Menu&gt;                             
                  &lt;/Grid&gt;
              &lt;/Border&gt;
  
              &lt;dg:DataGrid Grid.Row="1" x:Name="basketDG" Margin="5 0 5 0" Background="White"
                    AutoGenerateColumns="False" 
                    Style="{StaticResource DataGridStyle}"
                    SelectionMode="Extended"
                    GridLinesVisibility="None"
                    HeadersVisibility="Column" RowDetailsVisibilityMode="VisibleWhenSelected"
                    ItemsSource="{Binding BasketItems, Mode=OneWay}" CanUserAddRows="False" CanUserDeleteRows="False"
                    SelectionUnit="FullRow" SelectedItem="{Binding SelectedRelComplete}" 
                    VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"
                    SelectionChanged="BasketDgSelectionChanged"                  
                    Drop="DataGridDrop" 
                    DragEnter="DataGridDragEnter" 
                    AllowDrop="True"
                   &gt;
  
                  &lt;!-- Column definition --&gt;
                  &lt;dg:DataGrid.Columns&gt;
                      &lt;dg:DataGridTextColumn IsReadOnly="True" Width="100" Header="Symbol" Binding="{Binding Name}" &gt;
                          &lt;dg:DataGridTextColumn.ElementStyle&gt;
                              &lt;Style TargetType="{x:Type TextBlock}"&gt;
                                  &lt;Setter Property="TextWrapping" Value="Wrap" /&gt;
                              &lt;/Style&gt;
                          &lt;/dg:DataGridTextColumn.ElementStyle&gt;
                      &lt;/dg:DataGridTextColumn&gt;              
  
                      &lt;dg:DataGridTextColumn IsReadOnly="True" Width="*" Header="Symbol Description" Binding="{Binding Desc}" &gt;
                          &lt;dg:DataGridTextColumn.ElementStyle&gt;
                              &lt;Style TargetType="{x:Type TextBlock}"&gt;
                                  &lt;Setter Property="TextTrimming" Value="WordEllipsis" /&gt;
                              &lt;/Style&gt;
                          &lt;/dg:DataGridTextColumn.ElementStyle&gt;
                      &lt;/dg:DataGridTextColumn&gt;
  
                      &lt;dg:DataGridComboBoxColumn Width="200" Header="Column" 
                          SelectedValueBinding="{Binding Path=RelParams.ColumnName, UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="cName"
                          SelectedValuePath="cName"&gt;
                          &lt;dg:DataGridComboBoxColumn.ElementStyle&gt;
                              &lt;Style TargetType="ComboBox"&gt;
                                  &lt;Setter Property="ItemsSource" Value="{Binding RelInfo.Columns}" /&gt;
                              &lt;/Style&gt;
                          &lt;/dg:DataGridComboBoxColumn.ElementStyle&gt;
                          &lt;dg:DataGridComboBoxColumn.EditingElementStyle&gt;
                              &lt;Style TargetType="ComboBox"&gt;
                                  &lt;Setter Property="ItemsSource" Value="{Binding RelInfo.Columns}" /&gt;
                              &lt;/Style&gt;
                          &lt;/dg:DataGridComboBoxColumn.EditingElementStyle&gt;
                      &lt;/dg:DataGridComboBoxColumn&gt;
  
                  &lt;/dg:DataGrid.Columns&gt;               
  
              &lt;/dg:DataGrid&gt;
  
              &lt;Grid Grid.Row="2" Margin="0 5 0 0"&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*" &gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="Auto" &gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="5" &gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="Auto" &gt;&lt;/ColumnDefinition&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Grid.Column="0"&gt;
                      &lt;Button Name="BtnSR" Visibility="{Binding SelectedFunction,  Converter={StaticResource enumSRToVis}}" IsEnabled="{Binding ItemsExist}" Margin="2" Click="ShowBasketSettings"&gt;Basket Settings&lt;/Button&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Grid.Column="0"&gt;
                      &lt;Button Name="BtnCS" Visibility="{Binding SelectedFunction,  Converter={StaticResource enumCSToVis}}" IsEnabled="{Binding OnlyOneFutureItemExist}" Margin="2" Click="ShowCreateCurve"&gt;Curve Settings&lt;/Button&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Grid.Column="1"&gt;
                      &lt;Button Width="50" Name ="BtnClear" ToolTip="Clear Basket" Margin="2" IsEnabled="{Binding ItemsExist}"
                              Click="BtnClear_Click"&gt;Clear&lt;/Button&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Grid.Column="3"&gt;                 
                      &lt;Button Visibility ="{Binding ElementName=BtnSR, Path=Visibility}" 
                              ToolTip="Send Series Data to Table"
                              Name="SendToTable" Margin="2" Command="{Binding SendToTableCommand}" 
                              CommandParameter="{Binding ElementName=SendToTable}"&gt;Send to Table&lt;/Button&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Grid.Column="3"&gt;
                      &lt;Button Visibility="{Binding ElementName=BtnCS, Path=Visibility}" 
                              Name="CreateCurveSurface" Margin="2"                             
                              ToolTip="Send Curve Surface to Table"
                              IsEnabled="{Binding OnlyOneFutureItemExist}"
                              Click="CreateCurveSurfaceClick"
                      &gt;Send to Table&lt;/Button&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
      &lt;/GroupBox&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>Edit</strong>: here is stack trace </p>
  
  <blockquote>
    <p>Name:NullReferenceException Message:Object reference not set to an
    instance of an object. Target:Void
    RestoreAttachedItemValue(System.Windows.DependencyObject,
    System.Windows.DependencyProperty) Stack:   at
    Microsoft.Windows.Controls.DataGridRow.RestoreAttachedItemValue(DependencyObject
    objectWithProperty, DependencyProperty property)    at
    Microsoft.Windows.Controls.DataGridRow.SyncProperties(Boolean
    forcePrepareCells)    at
    Microsoft.Windows.Controls.DataGridRow.PrepareRow(Object item,
    DataGrid owningDataGrid)    at
    Microsoft.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject
    element, Object item)    at
    System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject
    container, Object item)    at
    System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject
    container)    at
    System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32
    childIndex, UIElement container, Boolean isRecycled)    at
    System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32
    childIndex, UIElement child, Boolean newlyRealized)    at
    System.Windows.Controls.VirtualizingStackPanel.BringIndexIntoView(Int32
    index)    at
    Microsoft.Windows.Controls.DataGrid.ScrollRowIntoView(Object item)<br>
    at Microsoft.Windows.Controls.DataGrid.OnScrollIntoView(Object arg)<br>
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
    callback, Object args, Int32 numArgs)    at
    MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
    source, Delegate method, Object args, Int32 numArgs, Delegate
    catchHandler)    at
    System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback,
    Object args, Int32 numArgs, Delegate catchHandler)    at
    System.Windows.Threading.DispatcherOperation.InvokeImpl()    at
    System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object
    state)    at System.Threading.ExecutionContext.runTryCode(Object
    userData)    at
    System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
    code, CleanupCode backoutCode, Object userData)    at
    System.Threading.ExecutionContext.RunInternal(ExecutionContext
    executionContext, ContextCallback callback, Object state)    at
    System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state, Boolean
    ignoreSyncCtx)    at
    System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state)    at
    System.Windows.Threading.DispatcherOperation.Invoke()    at
    System.Windows.Threading.Dispatcher.ProcessQueue()    at
    System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32
    msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)    at
    MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam,
    IntPtr lParam, Boolean&amp; handled)    at
    MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)    at
    System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
    callback, Object args, Int32 numArgs)    at
    MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
    source, Delegate method, Object args, Int32 numArgs, Delegate
    catchHandler)    at
    System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback,
    Object args, Int32 numArgs, Delegate catchHandler)    at
    System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority
    priority, TimeSpan timeout, Delegate method, Object args, Int32
    numArgs)    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,
    Int32 msg, IntPtr wParam, IntPtr lParam)    at
    MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)    at
    System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame
    frame)    at
    System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)<br>
    at System.Windows.Window.ShowHelper(Object booleanBox)    at
    System.Windows.Window.Show()    at System.Windows.Window.ShowDialog()</p>
  </blockquote>