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.
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;
}
}
|
var evnt = new EventToCommandBehavior()
{
EventName = StateChanged,
Command = command,
};
evnt.SetBinding(EventToCommandBehavior.CommandParameterProperty, ".");
control.Behaviors.Add(evnt); |