title: Data grid not update when data binding in xmal
body: <p>I have a model called <code>TimeSheetByEmployeeModel</code> that is implemented with <code>INotifyPropertyChanged</code>.</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public class TimeSheetByEmployeeModel:INotifyPropertyChanged
  {
      //public string EmpID { get; set; }
      string _EmpID;
      public string EmpID
      {
          get
          {
              return _EmpID;
          }
          set
          {
              if (_EmpID != value)
              {
                  _EmpID = value;
                  RaisePropertyChange("EmpID");
              }
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      public void RaisePropertyChange(string prop)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(prop));
  
          }
      }
  }
  </code></pre>
  
  <p><strong>ViewModel:</strong></p>
  
  <pre><code>ObservableCollection&lt;TimeSheetByEmployeeModel&gt; _TimeSheetList = new ObservableCollection&lt;TimeSheetByEmployeeModel&gt;();
  public ObservableCollection&lt;TimeSheetByEmployeeModel&gt; TimeSheetList
  {
      get
      {
          return _TimeSheetList;
      }
      set
      {                
          _TimeSheetList = value;             
      }
  }
  </code></pre>
  
  <p><strong>View Code-Behind:</strong></p>
  
  <pre><code>public TimeSheetByEmployee()
  {
      InitializeComponent();          
  
      this.DataContext = this;
  }
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>&lt;DataGrid  ItemsSource="{Binding TimeSheetList,UpdateSourceTrigger=PropertyChanged}"
             AutoGenerateColumns="False"
             CanUserAddRows="False"
             x:Name="timesheetgrid"
             HeadersVisibility="All"
             RowHeaderWidth="20"&gt;
  
    &lt;DataGrid.Columns&gt;
      &lt;DataGridTextColumn Binding="{Binding EmpID,UpdateSourceTrigger=PropertyChanged}" 
                      Header="EmpID"
                      IsReadOnly="True"
                      Width="100"/&gt;
    &lt;/DataGrid.Columns&gt;  
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>When I click a button inside the View it calls this method:</p>
  
  <pre><code>private void Ok_Click(object sender, RoutedEventArgs e)
  {       
      try
      {
          TimeSheetList.Clear();
          string EmployeeID = cboemployee.SelectedValue.ToString();
          TimeSheetList = TimeSheetByEmployeeDataAccessor.GetTimeSheetByEmployee_ByFromTo(dtpfrom.SelectedDate.GetValueOrDefault(), dtpto.SelectedDate.GetValueOrDefault(), EmployeeID);
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }          
  }
  </code></pre>
  
  <p>I dynamically changed <code>TimeSheetList</code> and it should reflect in the datagrid. I developed another form using this structure and it worked fine. However, in this form, when I change <code>TimeSheetList</code> it does not update the datagrid.</p>
  
  <p>What have I researched?</p>
  
  <p>Firstly, I bound some data to <code>TimeSheeList</code> in the view constructor and it's shown in the datagrid. So I assumed binding <code>TimeSheetList</code> to the datagrid works.</p>
  
  <p>Secondly, I changed some code in the Button Click Event:</p>
  
  <pre><code>private void Ok_Click(object sender, RoutedEventArgs e)
  {       
      try
      {      
          TimeSheetList.RemoveAt(0);
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }          
  }
  </code></pre>
  
  <p>This code is works perfectly and removes the first row in the datagrid. So I assumed the datagrid itemsource is dynamically changing.</p>
  
  <p>But why does it not update with my original code?</p>
  
  <p>More research,</p>
  
  <p>I added code to the button:</p>
  
  <pre><code>timesheetgrid.ItemsSource = TimeSheetList;
  </code></pre>
  
  <p>I know it will work, and it does, but normally I don't databind to the datagrid in code-behind and I always databind in the XAML. It always works, but why doesn't it work here?</p>
  
  <p><strong>Update</strong></p>
  
  <p>If I add <code>Mode=TwoWay</code> to the ItemsSource databinding in the datagrid in xaml while debugging like this:</p>
  
  <pre><code>&lt;DataGrid  
      ItemsSource="{Binding TimeSheetList,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
      AutoGenerateColumns="False"
      CanUserAddRows="False"
      x:Name="timesheetgrid"
      HeadersVisibility="All"
      RowHeaderWidth="20"&gt;
  </code></pre>
  
  <p>It works and the datagrid shows data. So I saved and restarted the debugging process and it stops working again.</p>