Collection of Xamarin CheckBox with Command event via c#

Hi guys,

Would like to have a help to render checkboxlist with command and command parameter via c# code.

I need the equivalent of XAML code "{Binding .}"


See my attached file for the sample control generator i used. 




Attachment: CreateCheckboxList_8f8a4f0c.rar

3 Replies

YA Yas November 25, 2021 08:16 AM UTC

Sample code:


public async Task<StackLayout> CreateCheckboxList(ControlModel model, ICommand command = null)

        {

            try

            {

                var inputHelper = new ControlDto()

                {

                    Name = model.ID,

                    ID = model.ID,

                    Label = model.Label,

                    ControlType = model.ControlType,

                };


                var source = new ObservableCollection<DataSourceDto>

                {

                    new DataSourceDto(){DisplayText = "Option 1", ID = 1, IsChecked = false },

                    new DataSourceDto(){DisplayText = "Option 2", ID = 2, IsChecked = false },

                    new DataSourceDto(){DisplayText = "Option 3", ID = 3, IsChecked = false },

                    new DataSourceDto(){DisplayText = "Option 4", ID = 4, IsChecked = false },

                    new DataSourceDto(){DisplayText = "Option 5", ID = 5, IsChecked = false },

                };


                var layout = new StackLayout();


                var template = new DataTemplate(() =>

                {

                    var control = new SfCheckBox();

                    var grid = new Grid();


                    control.SetBinding(SfCheckBox.TextProperty, "DisplayText");

                    control.SetBinding(SfCheckBox.IsCheckedProperty, "IsChecked");


                    //==NEED THE EQUIVALENT OF XAML CODE "{Binding .}"

                    //var evnt = new EventToCommandBehavior()

                    //{

                    // EventName = EventName.StateChanged,

                    // Command = command,

                    // CommandParameter = "{Binding .}",

                    //};


                    //control.Behaviors.Add(evnt);


                    grid.Children.Add(control);


                    return grid;

                });


                BindableLayout.SetItemsSource(layout, source);

                BindableLayout.SetItemTemplate(layout, template);

                return await Task.FromResult(layout);

            }

            catch (Exception ex)

            {

                Debug.WriteLine($"{ex.GetType().Name + " : " + ex.Message}");

                throw ex;

            }

        }



GM Gayathri Manickam Syncfusion Team November 28, 2021 12:31 PM UTC

Hi Abdiel, 
  
Greetings from Syncfusion. 
 
This is a general update to let you know that our support team has taken out your query and we are validating the feasibility to achieve your requirement. We will update you the complete status on or before 30th November 2021.   
 
Thanks,  
Gayathri M. 



GM Gayathri Manickam Syncfusion Team December 1, 2021 02:41 AM UTC

Hi Abdiel, 
 
We analyzed your query “Equivalent c# code for “{Binding.}” “, and we would like to let you know that "{Binding .}" simply equals to new Binding(".") in c#. Please see below code snippet for you reference. 
 
var evnt = new EventToCommandBehavior() 
 
         EventName = StateChanged, 
         Command = command, 
  }; 
 evnt.SetBinding(EventToCommandBehavior.CommandParameterProperty, "."); 
 control.Behaviors.Add(evnt); 
 
Please let us know if you need further assistance on this. 
 
Thanks,  
Gayathri M. 


Loader.
Up arrow icon