title: BindingGroup in ListView with DataTemplate
body: <p>I have recently found out about <code>BindingGroups</code>, and it seem rather nice.
  But I can't get it to work in my <code>ListView</code> that is filled with data represented in <code>DataTemplates</code>.</p>
  
  <p>(In advance I just like to say: sorry for all the code, it's just to give you a better understanding)</p>
  
  <p>So in XAML it looks something like this:</p>
  
  <pre><code>&lt;StackPanel Name="stpData"&gt;
      &lt;StackPanel.BindingGroup&gt;
          &lt;BindingGroup /&gt;
      &lt;/StackPanel.BindingGroup&gt;
  
      &lt;!-- This works nice! --&gt;
      &lt;TextBox Text="{Binding Path=name}" /&gt;
      &lt;CheckBox IsChecked="{Binding Path=enable}" /&gt;
  
      &lt;!-- This doesn't work so nice... --&gt;
      &lt;ListView Name="lvBools" ItemsSource="{Binding Path=myList}"&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn&gt;
                      &lt;GridViewColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;StackPanel&gt;
                                  &lt;CheckBox IsChecked="{Binding bools}" /&gt;
                              &lt;/StackPanel&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/GridViewColumn.CellTemplate&gt;
                  &lt;/GridViewColumn&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>And code-behind:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
  
      TestData Data = new TestData()
      {
          name = "Test",
          enable = true,
          myList = new List&lt;TestData.myItem&gt; 
          {
              new TestData.myItem(true), 
              new TestData.myItem(true), 
              new TestData.myItem(false) 
          },
      };
  
      stpData.DataContext = Data;
      stpData.BindingGroup.BeginEdit();
  }
  
  private void btnSave_Click(object sender, RoutedEventArgs e)
  {
      stpData.BindingGroup.CommitEdit();
      stpData.BindingGroup.BeginEdit();
  }
  
  private void btnCancel_Click(object sender, RoutedEventArgs e)
  {
      stpData.BindingGroup.CancelEdit();
      stpData.BindingGroup.BeginEdit();
  }
  
  public class TestData
  {
      public class myItem
      {
          public myItem(bool b)
          {
              this.bools = b;
          }
          public bool bools { get; set; }
      }
  
      public string name { get; set; }
      public bool enable { get; set; }
      public List&lt;myItem&gt; myList { get; set; }
  }
  </code></pre>
  
  <p>So what happends it that when I change the name, or press the enable CheckBox, those changes won't be submitted until I press the SaveButton (btnSave_Click) (not included in the <code>XAML</code> )
  Or if I press a Cancel button changes is "restored".
  But if I click one of the checkBoxes in the ListView, those changes will be submitted immediately.</p>
  
  <p>My own guess: The ListView (or maybe even the <code>StackPanel</code> in the <code>DataTemplate</code>) stops the <code>BindingGroup</code> inheritance chain. But in that case, how can I include them?</p>
  
  <p><strong>EDIT</strong>
  I have also tried to add a new <code>BindingGroup</code> to the DataTemplate:</p>
  
  <pre><code>&lt;DataTemplate&gt;
      &lt;StackPanel x:Name="SPpan"&gt;
          &lt;StackPanel.BindingGroup&gt;
              &lt;BindingGroup Name="BGStackPanel" /&gt;
          &lt;/StackPanel.BindingGroup&gt;
          &lt;CheckBox IsChecked="{Binding Path=bools}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Now when I edit the <code>bools</code> (Click them in my <code>ListView</code> ), the source doesn't get updated, and that's good, but now I can find no way to save (<em>Commit</em>) the data.
  I can't access neither <code>SPpan</code> nor <code>BGStackPanel</code> from code-behind.
  However I have tried the following:</p>
  
  <pre><code>var tmp = (lvBools.View as GridView).Columns[0];
  (tmp.CellTemplate.LoadContent() as StackPanel).BindingGroup.BeginEdit();
  </code></pre>
  
  <p>but also this without success...</p>
  
  <p><strong>EDIT</strong>
  Ok, so edit II... So I guess that the reason to why the source doesn't update is because I'm running the Begin/CommitEdit on the Template and not on the actual objects created from the template. So does anyone know how to reach those objects?</p>