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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

SfCheckbox data binding in C#

Thread ID:

Created:

Updated:

Platform:

Replies:

150675 Jan 15,2020 07:22 PM UTC Jan 23,2020 06:01 AM UTC Xamarin.Forms 5
loading
Tags: SfCheckBox
david schneider
Asked On January 15, 2020 07:22 PM UTC

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
                    };


Geetha Rajendran [Syncfusion]
Replied On 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.


david schneider
Replied On 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);
                }

Hemalatha Marikumar [Syncfusion]
Replied On 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. 
 
 
 


david schneider
Replied On January 22, 2020 03:48 PM UTC

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

Hemalatha Marikumar [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon