I have a ObservableCollection of Category which has a selected property and I'm dynamically creating a series of checkboxes which I want to two way bind.
I populate the checkboxes correctly, and when I populate the form these will appear correctly bound. However I want to be able to programatically set the checkboxes and was assuming I could change the state of the item in the collection and the checkbox would reflect this but that was not the case. I could do it the other way around and programtically determine the control was a checkbox and then set its state but that seemed more complicated and did not seem to reflect what I thought should be happening with 2 way binding. Any ideas what I'm missing on setting up the binding.
public partial class CategorySelect : ContentPage
{
public ObservableCollection<Category> SelectedCategories { get; set; }
public CategorySelect(ObservableCollection<Category> list)
{
InitializeComponent();
SelectedCategories = list;
//Create a list of check boxes for each category and bind to the collection
int index = 0;
foreach (var item in SelectedCategories)
{
var b = new Syncfusion.XForms.Buttons.SfCheckBox() { Text = item.Description };
b.BindingContext = SelectedCategories[index];
b.SetBinding(Syncfusion.XForms.Buttons.SfCheckBox.IsCheckedProperty, "Selected", mode: BindingMode.TwoWay);
SelectionList.Children.Add(b);
index++;
}
private void Button2_Clicked(object sender, EventArgs e)
{
foreach (var item in SelectedCategories)
{
item.Selected = true;
}
}
private void Button_Clicked(object sender, EventArgs e)
{
// Should be able to better do this with 2 way binding between the collection and the
// controls that are added - meaning we should just be able to modify the collection.
foreach (var item in SelectionList.Children)
{
if (item.GetType() == typeof(Syncfusion.XForms.Buttons.SfCheckBox))
{
((Syncfusion.XForms.Buttons.SfCheckBox)item).IsChecked = true;
}
}
}
}