title: I cant add new data to DataGrid
body: <p>I have been searching issues like this and making a lot of changes in my code but I can't solve it.</p>
  <p>I have created a DataGrid:</p>
  <pre><code>    &lt;DataGrid Grid.Row=&quot;1&quot; ItemsSource=&quot;{Binding VarDt}&quot;  AutoGenerateColumns=&quot;False&quot; &gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header=&quot;Name&quot; Binding=&quot;{Binding Name}&quot;/&gt;
              &lt;DataGridTextColumn Header=&quot;ID&quot; Binding=&quot;{Binding Id}&quot;/&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  <p>The list of the class that Im using is in the Main model.</p>
  <pre><code>    private List&lt;ObjectVariable&gt; _varDt;
      public List&lt;ObjectVariable&gt; VarDt
      {
          get { return _varDt; }
          set 
          { 
              _varDt = value;
              OnPropertyChanged(&quot;VarDt&quot;);
          }
      }
  </code></pre>
  <p>When I add the data for the first time it works and I can see the values in my DataGrid:</p>
  <pre><code>        public MainViewModel()
          {
          VarDt = new List&lt;ObjectVariable&gt;()
          {
              new ObjectVariable { Id=&quot;9846849&quot;, Name=&quot;Val1&quot; },
              new ObjectVariable { Id=&quot;fregreg&quot;, Name=&quot;Val2&quot; },
              new ObjectVariable { Id=&quot;cd6s8s6&quot;, Name=&quot;Val3&quot; }
          }; [...]
  </code></pre>
  <p>But when I add new items when i execute a command (when i tree item is double clicked), the DataGrid doen's update.
  (code where I add the new item that it doens't work):</p>
  <pre><code>    private ICommand _selectItemCommand;
      public ICommand selectItemCommand
      {
          get
          {
              return _selectItemCommand ?? (_selectItemCommand = new RelayCommand(param =&gt; this.LoadContent(param)));
          }
      }
  
      private void LoadContent(object selectedMenuItem)
      {
          // NEW ITEM THAT IS NOT ADDED WHY???
          VarDt.Add(new ObjectVariable { Id = &quot;cw61851cw&quot;, Name = &quot;ValPost&quot; });          
      }
  </code></pre>
  <p>I dont know if I am not raising the PropertyChanged event correctly and how I can do it.</p>
  <p>Someone can tell me how can I add data correctly to my DataGrid.</p>
  <p>Thanks!</p>
  <p><strong>Edit 1: Still not working with observable collection</strong></p>
  <p>Declaration:</p>
  <pre><code>        private ObservableCollection&lt;ObjectVariable&gt; _varDt;
      public ObservableCollection&lt;ObjectVariable&gt; VarDt
      {
          get { return _varDt; }
          set 
          { 
              _varDt = value;
              OnPropertyChanged(&quot;VarDt&quot;);
          }
      }
  </code></pre>
  <p>Public MainViewModel():</p>
  <pre><code>            VarDt = new ObservableCollection&lt;ObjectVariable&gt;()
          {
              new ObjectVariable { Id=&quot;9846849&quot;, Name=&quot;ValuesAtStart1&quot; },
              new ObjectVariable { Id=&quot;fregreg&quot;, Name=&quot;ValuesAtStart2&quot; },
              new ObjectVariable { Id=&quot;cd6s8s6&quot;, Name=&quot;ValuesAtStart3&quot; }
          };
  </code></pre>
  <p>Command:</p>
  <pre><code>        private void LoadContent(object selectedMenuItem)
      {
  
              // NEW ITEM THAT IS NOT ADDED WHY???
              VarDt.Add(new ObjectVariable { Id = &quot;cw61851cw&quot;, Name = &quot;ValueInsideCommand&quot; });
      }
  </code></pre>
  <p>Result (data from command not added):</p>
  <p><a href="https://i.stack.imgur.com/kfbzx.png" rel="nofollow noreferrer">Value inside command not added</a></p>
  <p><strong>Edit 2: Problem found but I dont have the solution</strong></p>
  <p>The DataGrid where I have the problem is inside another View that I manage with another class and a ContentControl:</p>
  <p>Actually I have:</p>
  <ul>
  <li>MainViewModel and the MainView.</li>
  <li>VariablesViewModel and VariablesView that i put inside the ContentControl.
  The VariablesViewModel is empty, I only use it to show the VariablesView.</li>
  </ul>
  <p>When I execute a command, I show the VariablesView in the ContentControl:</p>
  <pre><code>        private void LoadContent(object selectedMenuItem)
      {
          TreeItemModel ItemSelected = (TreeItemModel)selectedMenuItem;
  
          if (ItemSelected.Name == &quot;Object variables&quot;)
          {
              // Current view is VariablesView
              CurrentView = VariableVM;
  
              // NEW ITEM THAT IS NOT ADDED WHY???
              VarDt.Add(new ObjectVariable { Id = &quot;cw61851cw&quot;, Name = &quot;ValueInsideCommand&quot; });
  
          }
              
          else CurrentView = &quot;&quot;;
      }
  </code></pre>
  <p>I realized that the DataGrid inside the VariablesView that I show in the ContentControl doesn't update the values, but if I put the same DataGrid in the Main View, the values are updated.</p>
  <p>Maybe the problem is that &quot;VarDt&quot; is in the MainViewModel, not in VariablesViewModel, but I can show some data, (only the new values are not showed)</p>
  <p>Test code:</p>
  <pre><code>                [...]
              &lt;ContentControl Grid.Column=&quot;1&quot;
                              Margin=&quot;10&quot;
                              Content=&quot;{Binding CurrentView}&quot;/&gt;
  
              &lt;DataGrid Grid.Row=&quot;3&quot; Grid.Column=&quot;2&quot; ItemsSource=&quot;{Binding VarDt}&quot;  AutoGenerateColumns=&quot;True&quot; /&gt;
              [...]
  </code></pre>
  <p>Result of test Code:
  <a href="https://i.stack.imgur.com/NFKEa.png" rel="nofollow noreferrer">DataBinding result using ContentControl os DataGrid in MainView</a></p>
  <p><strong>Edit 3: Full context</strong></p>
  <p>Variables view:</p>
  <pre><code>&lt;UserControl x:Class=&quot;AOE.MVVM.View.VariablesView&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
           xmlns:viewmodel=&quot;clr-namespace:AOE.MVVM.ViewModel&quot;  
           xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
           xmlns:local=&quot;clr-namespace:AOE.MVVM.View&quot;
           mc:Ignorable=&quot;d&quot; 
           d:DesignHeight=&quot;450&quot; d:DesignWidth=&quot;800&quot;&gt;
  &lt;!--Asignación del archivo .cs a esta view--&gt;
  &lt;UserControl.DataContext&gt;
      &lt;viewmodel:MainViewModel/&gt;
  &lt;/UserControl.DataContext&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions &gt;
          &lt;RowDefinition Height=&quot;30*&quot; MaxHeight=&quot;70&quot;/&gt;
          &lt;RowDefinition Height=&quot;100*&quot;/&gt;
          &lt;RowDefinition Height=&quot;10*&quot; MaxHeight=&quot;30&quot;/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;StackPanel Grid.Row=&quot;0&quot;&gt;
          &lt;Image Source=&quot;/Media/Variables.png&quot;
                 Height=&quot;50&quot;
                 Width=&quot;50&quot;
                 HorizontalAlignment=&quot;Left&quot;/&gt;
          &lt;TextBlock Text=&quot;Variables&quot;
                     FontFamily=&quot;Verdana&quot;
                     Foreground=&quot;#007BC0&quot;/&gt;
  
      &lt;/StackPanel&gt;
      &lt;DataGrid Grid.Row=&quot;1&quot; ItemsSource=&quot;{Binding VarDt}&quot;  AutoGenerateColumns=&quot;True&quot; &gt;
  
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>VariablesViewModel:
  Empty class.</p>
  <p>AppXaml:</p>
  <pre><code>&lt;Application x:Class=&quot;AOE.App&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:local=&quot;clr-namespace:AOE&quot;
           xmlns:viewModel=&quot;clr-namespace:AOE.MVVM.ViewModel&quot;
           xmlns:view=&quot;clr-namespace:AOE.MVVM.View&quot;
           StartupUri=&quot;MainWindow.xaml&quot;&gt;
  &lt;Application.Resources&gt;
      
      &lt;DataTemplate DataType=&quot;{x:Type viewModel:VariablesViewModel}&quot;&gt;
          &lt;view:VariablesView/&gt;
      &lt;/DataTemplate&gt;
  
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p>MainViewModel:</p>
  <pre><code>namespace AOE.MVVM.ViewModel
  {
  class MainViewModel : ObservableObject
  {
  
      public ObservableCollection&lt;ObjectVariable&gt; VarDt { get; set; }
  
      //public TreeItemModel ItemSelected { get; set; }
  
      private ICommand _selectItemCommand;
      public ICommand selectItemCommand
      {
          get
          {
              return _selectItemCommand ?? (_selectItemCommand = new RelayCommand(param =&gt; this.LoadContent(param)));
          }
      }
  
      private void LoadContent(object selectedMenuItem)
      {
  
          // Current view is VariablesView
          CurrentView = VariableVM;
  
          // NEW ITEM THAT IS NOT ADDED WHY???
          VarDt.Add(new ObjectVariable { Id = &quot;cw61851cw&quot;, Name = &quot;ValueInsideCommand&quot; });
  
      }
  
      public VariablesViewModel VariableVM { get; set; }
  
      private object _currentView;
  
      public object CurrentView
      {
          get { return _currentView; }
          set
          {
              _currentView = value;
              OnPropertyChanged(&quot;CurrentView&quot;);
          }
      }
  
      public MainViewModel()
      {
  
          MainTree.Add(MainObject);
  
          VarDt = new ObservableCollection&lt;ObjectVariable&gt;()
          {
              new ObjectVariable { Id=&quot;9846849&quot;, Name=&quot;ValuesAtStart1&quot; },
              new ObjectVariable { Id=&quot;fregreg&quot;, Name=&quot;ValuesAtStart2&quot; },
              new ObjectVariable { Id=&quot;cd6s8s6&quot;, Name=&quot;ValuesAtStart3&quot; }
          };
  
          VariableVM = new VariablesViewModel();
  
      }
    }
  }
  </code></pre>