I have recently found out about BindingGroups, and it seem rather nice.
But I can't get it to work in my ListView that is filled with data represented in DataTemplates.
(In advance I just like to say: sorry for all the code, it's just to give you a better understanding)
So in XAML it looks something like this:
<StackPanel Name="stpData">
<StackPanel.BindingGroup>
<BindingGroup />
</StackPanel.BindingGroup>
<!-- This works nice! -->
<TextBox Text="{Binding Path=name}" />
<CheckBox IsChecked="{Binding Path=enable}" />
<!-- This doesn't work so nice... -->
<ListView Name="lvBools" ItemsSource="{Binding Path=myList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding bools}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
And code-behind:
public MainWindow()
{
InitializeComponent();
TestData Data = new TestData()
{
name = "Test",
enable = true,
myList = new List<TestData.myItem>
{
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<myItem> myList { get; set; }
}
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 XAML )
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.
My own guess: The ListView (or maybe even the StackPanel in the DataTemplate) stops the BindingGroup inheritance chain. But in that case, how can I include them?
EDIT
I have also tried to add a new BindingGroup to the DataTemplate:
<DataTemplate>
<StackPanel x:Name="SPpan">
<StackPanel.BindingGroup>
<BindingGroup Name="BGStackPanel" />
</StackPanel.BindingGroup>
<CheckBox IsChecked="{Binding Path=bools}" />
</StackPanel>
</DataTemplate>
Now when I edit the bools (Click them in my ListView ), the source doesn't get updated, and that's good, but now I can find no way to save (Commit) the data.
I can't access neither SPpan nor BGStackPanel from code-behind.
However I have tried the following:
var tmp = (lvBools.View as GridView).Columns[0];
(tmp.CellTemplate.LoadContent() as StackPanel).BindingGroup.BeginEdit();
but also this without success...
EDIT 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?