We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

SfCheckbox data binding in C#

All the examples I've seen so far are xaml based data binding.  I have a dynamically built list of checkboxs and wondering how to initialize data binding in C#

I can't figure out how to translate the xaml example into C#.    Thanks!

                    var binding = new Binding();
                    binding.Mode = BindingMode.TwoWay;
                    binding.Source = filter.Value.Active;

                    var checkbox = new SfCheckbox()
                    {
                        Text = filter.Value.Title,
                        IsChecked = binding
                    };


5 Replies

GR Geetha Rajendran Syncfusion Team January 17, 2020 06:51 AM UTC

Hi David,

 

Query: how to initialize data binding in C#

We have prepared a simple sample to bind the properties of SfCheckBox in code behind. Please refer the below code.

 

Code snippet [C#]:

var binding = new Binding()

{

    Source = viewModel,

    Path = "IsActive",

    Mode = BindingMode.TwoWay

};

 

SfCheckBox sfCheckBox = new SfCheckBox();

sfCheckBox.SetBinding(SfCheckBox.IsCheckedProperty, binding);

 

Sample -  https://www.syncfusion.com/downloads/support/forum/150675/ze/150675-sample-365345168.zip

 

Please get back to us if you need further assistance on this.

 

Regards,

Geetha R.



DS david schneider January 17, 2020 02:27 PM UTC

Thanks.  How about if the data is buried in a collection.   Here is a snippet using your example in my code.  The commented out lines is the old-school way of fielding the state change events to update the backing store.  Maybe you can't bind on a local variable that points to the backing store.

                foreach (var filter in filters.GetAllFilters(category.Value.Key))
                {
                    var binding = new Binding()
                    {
                        Source = "filter",
                        Path = "Value.FilterActive",
                        Mode = BindingMode.TwoWay
                    };

                    var checkbox = new SfCheckBox()
                    {
                        Text = filter.Value.Title,
                        //IsChecked = filter.Value.FilterActive,
                        CheckedColor = Color.Black,
                        FontSize = 20,
                    };
                    checkbox.SetBinding(SfCheckBox.IsCheckedProperty, binding);

                    //checkbox.StateChanged += Checkbox_StateChanged;
                    radiogroup.Children.Add(checkbox);
                }


HM Hemalatha Marikumar Syncfusion Team January 22, 2020 03:13 AM UTC

Hi David, 
 
Thanks for your update. 
 
We have validated the provided code snippet and prepared sample as per your requirement which is worked fine even binding with path and source from other collection and backing store also worked as per in below code snippet 
 
Code Snippet [C#]: 
  foreach(Model model in viewModel.Data) 
            { 
               var binding = new Binding() 
                { 
                    Source = model, 
                    Path = "IsChecked", 
                    Mode = BindingMode.TwoWay 
                }; 
 
                Binding textbinding = new Binding() 
                { 
                    Source = model, 
                    Path = "Name", 
                    Mode = BindingMode.TwoWay 
                }; 
 
                SfCheckBox sfCheckBox = new SfCheckBox(); 
                sfCheckBox.SetBinding(SfCheckBox.TextProperty, textbinding); 
                sfCheckBox.SetBinding(SfCheckBox.IsCheckedProperty, binding); 
                layout.Children.Add(sfCheckBox); 
            } 
 
 
Note: In your code snippet, you have mentioned the source value as string. 
 
Please check the below modified sample and let us know if you have any other concern. 
 
 
If that is not meet your requirement update us with modified sample to check further. 
 
Regards, 
Hemalatha M. 
 
 
 



DS david schneider January 22, 2020 03:48 PM UTC

Thanks!   It works perfectly. Your technical support is awesome!  


HM Hemalatha Marikumar Syncfusion Team January 23, 2020 06:01 AM UTC

Hi David, 
 
Thanks for your update. 
 
We are glad to hear that given solution works.  
 
Please let us know if you need any further assistance. 
 
Regards, 
Hemalatha M. 


Loader.
Live Chat Icon For mobile
Up arrow icon