title: WPF DataGrid TemplateColumn with CheckBoxes and Select All -checkbox in header using MVVM Light
body: <p>i'm trying to create a DataGrid with a column of checkboxes. On that CheckBox-column's header, i'd like to have a "check/uncheck all"-type of checkbox.</p>
  
  <p>App is using WPF, .NET 4 and MVVM Light.</p>
  
  <p>Here's the code:</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;DataGrid x:Name="dgReportList" ItemsSource="{Binding Path=ReportListItems}"&gt;
  &lt;DataGrid.Columns&gt;
      &lt;DataGridTextColumn Header="*SomeText"  Binding="{Binding Path=SourceReport.Name}" /&gt;
      &lt;DataGridTemplateColumn&gt;
          &lt;DataGridTemplateColumn.Header&gt;
              &lt;CheckBox IsChecked="{Binding Source={StaticResource Locator},     
                    Path=MainWindowViewModel.CheckAll, Mode=TwoWay, 
                    UpdateSourceTrigger=PropertyChanged}"  /&gt;
          &lt;/DataGridTemplateColumn.Header&gt;
          &lt;DataGridTemplateColumn.CellTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, 
               UpdateSourceTrigger=PropertyChanged}"&gt;                                    
            &lt;/CheckBox&gt;
              &lt;/DataTemplate&gt;
          &lt;/DataGridTemplateColumn.CellTemplate&gt;
     &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p>...</p>
  
  <p>ViewModel:</p>
  
  <pre><code>    private ObservableCollection&lt;ReportListItemModel&gt; _reportListItems;
  
      public ObservableCollection&lt;ReportListItemModel&gt; ReportListItems
      {
          get
          {
              return this._reportListItems;
          }
          private set
          {
              this._reportListItems = value;
              this.RaisePropertyChanged("ReportListItems");
          }
      }
  
      ....
  
      public bool CheckAll
      {
          get { return this._checkall; }
          set 
          {
              this._checkall = value; 
  
              foreach (var reportListItemModel in ReportListItems)
              {
                  reportListItemModel.IsSelected = this._checkall;
              }
          }
      }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public class ReportListItemModel
  {
      public EnvironmentConfigurationModel TargetConfigurationModel { get; set; }
      public ReportModel TargetReport { get; set; }
  
      public EnvironmentConfigurationModel SourceConfigurationModel { get; set; }
      public ReportModel SourceReport { get; set; }
  
      private bool _isSelected;
  
      public bool IsSelected
      {
          get { return _isSelected; }
          set 
          {
              _isSelected = value;
          }
      }
  }
  </code></pre>
  
  <p>My idea is to bind the DataGrid to a ObservableCollection of type ReportListItemModel. ReportListItemModel contains a public boolean property "IsSelected", which i want to get bound to the checkbox.</p>
  
  <hr>
  
  <p><strong>Scenario:</strong><br>
  As a user, i want to be able to select (or deselect) all the rows by clicking on the checkbox located at the header row.</p>
  
  <hr>
  
  <p><strong>Tests:</strong><br>
  Task: Click the "Header-Checkbox", when it's state is unchecked.<br>
  Expected: All checkboxes on individual rows get checked.<br>
  Actual: Only the checkbox on header row gets checked.</p>
  
  <p>Task: Click the "Header-Checkbox", when it's state is checked.<br>
  Expected: All checkboxes on individual rows get unchecked.<br>
  Actual: Only the checkbox on header row gets unchecked.    </p>
  
  <hr>
  
  <p>Both the "select all"-checkbox and the checboxes on the rows cause the property within model to be set as expected. This just doesn't get bound to the View. I have a creepy feeling that i have my model and viewmodel set funny somehow, even though the DataGridTextColumn does get the value from model a-ok.</p>
  
  <p>I'm happy to provide any additional code or info required! </p>
  
  <p>N.B. I'm just starting up with WPF, MVVM etc., so if there's some fundamental flaw here, i'd be happy to hear about it.</p>
  
  <p>Edit:<br>
  Edited for readability...</p>