Reputation: 3375
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?
Upvotes: 2
Views: 1547
Reputation: 3375
Ok, so thanks to Mr E.L Dunn I mannage to solve this one!
So in my first EDIT I suggested that I could maybe add a BindingGroup
to the StackPanel
in the DataTemplate
, and that was kind of in the right direction...
All that's needed to be done is to add a BindingGroupName
to the DataTemplate's CheckBox.
<StackPanel Name="stpData">
<StackPanel.BindingGroup>
<BindingGroup Name="bindingGroup"/>
</StackPanel.BindingGroup>
<!-- This works nice! -->
<TextBox Text="{Binding Path=name}" />
<CheckBox IsChecked="{Binding Path=enable}" />
<!-- Now this work nice as well! -->
<ListView Name="lvBools" ItemsSource="{Binding Path=myList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding bools, BindingGroupName=bindingGroup}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
Simple as that!
Upvotes: 1
Reputation: 3180
You could place the various sub items in blocks of related radio buttons. Then put each group in a panel or a multiview. Then have the main radiolist do auto postback. On the postback read which radiobutton was clicked/selected then show the relevant sub group by revealing the relevant panel or view.
If you place these inside an updatepanel, you can do all of this without an obcious page refresh.
Upvotes: 0