title: wpf DataGrid SelectedItem get NullReferenceException after binding
body: <p><strong>Finally</strong></p>
  
  <blockquote>
    <p>I discover property "Item" maybe reuse of SelectedItem = {Binding
    Item}, so it always get exception.</p>
  </blockquote>
  
  <p>I had a DataGrid, include expander.</p>
  
  <p><strong>Edit</strong> <em>When I want to use SelectedItem, I get "NullReferenceException".</em></p>
  
  <p>When I want to do something, like UnSelectAll or Collection.Remove SelectedItem, I get "NullReferenceException".</p>
  
  <p><strong>Example is called UnSelectAll stacktrace</strong></p>
  
  <pre><code>---------------------------
     於 MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid(Object item)
  
     於 MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
  
     於 MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
  
     於 MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate()
  
     於 System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
  
     於 System.Windows.Data.BindingExpressionBase.UpdateValue()
  
     於 System.Windows.Data.BindingExpression.UpdateOverride()
  
     於 System.Windows.Data.BindingExpressionBase.Update()
  
     於 System.Windows.Data.BindingExpressionBase.ProcessDirty()
  
     於 System.Windows.Data.BindingExpressionBase.Dirty()
  
     於 System.Windows.Data.BindingExpressionBase.SetValue(DependencyObject d, DependencyProperty dp, Object value)
  
     於 System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  
     於 System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
  
     於 System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
  
     於 System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
  
     於 System.Windows.Controls.Primitives.Selector.UnselectAllImpl()
  
     於 System.Windows.Controls.DataGrid.UnselectAllCells()
  
     於 SList.Expander_Click(Object sender, RoutedEventArgs e) 於 D:\SList.xaml.cs: 行 123, System.NullReferenceException: 並未將物件參考設定為物件的執行個體。
  
     於 MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid(Object item)
  
     於 MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
  
     於 MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
  
     於 MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate()
  
     於 System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
  
     於 System.Windows.Data.BindingExpressionBase.UpdateValue()
  
     於 System.Windows.Data.BindingExpression.UpdateOverride()
  
     於 System.Windows.Data.BindingExpressionBase.Update()
  
     於 System.Windows.Data.BindingExpressionBase.ProcessDirty()
  
     於 System.Windows.Data.BindingExpressionBase.Dirty()
  
     於 System.Windows.Data.BindingExpressionBase.SetValue(DependencyObject d, DependencyProperty dp, Object value)
  
     於 System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  
     於 System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
  
     於 System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
  
     於 System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
  
     於 System.Windows.Controls.Primitives.Selector.UnselectAllImpl()
  
     於 System.Windows.Controls.DataGrid.UnselectAllCells()
  
     於 SList.Expander_Click(Object sender, RoutedEventArgs e) 於 D:\SList.xaml.cs: 行 123
  ---------------------------
  </code></pre>
  
  <p>But SelectedIndex work well.</p>
  
  <p>I don't know what happening, but I need binding SelectedItem.</p>
  
  <p>please help!</p>
  
  <p><strong>My ViewModel</strong></p>
  
  <pre><code>public class TestVM : INotifyPropertyChanged
  {
      private int _index;
      public int Index
      {
          get
          {
              return _index;
          }
          set
          {
              _index = value;
              RaisePropertyChanged("Index");
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      private void RaisePropertyChanged(String propertyName)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
  
      private User _item;
      public User Item
      {
          get
          {
              return _item;
          }
          set
          {
              _item = value;
              RaisePropertyChanged("Item");
          }
      }
  
      ObservableCollection&lt;User&gt; _collection;
      public ObservableCollection&lt;User&gt; Collection
      {
          get
          {
              return _collection;
          }
          private set
          {
              _collection = value;
              RaisePropertyChanged("Collection");
          }
      }
  
      ListCollectionView _groupView;
      public ListCollectionView GroupView
      {
          get
          {
              return _groupView;
          }
          private set
          {
              _groupView = value;
              RaisePropertyChanged("GroupView");
          }
      }
  
      public TestVM()
      {
          Collection = new ObservableCollection&lt;User&gt;();
          Collection.Add(new User() { Name = "John Doe1", Age = 10, group = "Group 1" });
          Collection.Add(new User() { Name = "Jane Doe2", Age = 20, group = "Group 1" });
  
          Collection.Add(new User() { Name = "Sammy Doe", Age = 30, group = "Group 2" });
          Collection.Add(new User() { Name = "Sammy Doe1", Age = 40, group = "Group 2" });
          Collection.Add(new User() { Name = "Sammy Doe2", Age = 50, group = "Group 2" });
  
          Collection.Add(new User() { Name = "Sammy Doe3", Age = 60, group = "Group 3" });
          Collection.Add(new User() { Name = "Sammy Doe4", Age = 70, group = "Group 3" });
          Collection.Add(new User() { Name = "Sammy Doe5", Age = 80, group = "Group 3" });
  
          GroupView = new ListCollectionView(Collection);
          GroupView.GroupDescriptions.Add(new PropertyGroupDescription("group"));
      }
  }
  
  public class User
  {
      public string Name { set; get; }
      public int Age { set; get; }
  
      public string group { get; set; }
  }
  </code></pre>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;DataGrid Name="dg" ItemsSource="{Binding Collection}" SelectedIndex="{Binding Index, Mode=TwoWay}" SelectedItem="{Binding Item, Mode=TwoWay}"/&gt;
  </code></pre>