How to create a collection of .NET MAUI CheckBox (SfCheckBox)?
This article describes how to create the collection of .NET MAUI CheckBox. Let’s consider an example to create a collection that showcases CheckBoxes for various cities. The ItemsSource
property is utilized to bind the collection of items, and the ItemTemplate
, specifies the display format for each item, using the CityName property as the text. Additionally, the BindingContext
is used to connect the ViewModel class to the View.
The following code explains how to create a collection of .NET MAUI CheckBox:
XAML:
<ContentPage.BindingContext>
<local:ViewModel/>
</ContentPage.BindingContext>
<StackLayout BindableLayout.ItemsSource="{Binding Cities}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<buttons:SfCheckBox Text="{Binding CityName}" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
C#:
public class City
{
public string CityName { get; set; }
}
public class ViewModel
{
private ObservableCollection<city> cities;
public ObservableCollection<city> Cities
{
get { return cities; }
set { cities = value; }
}
public ViewModel()
{
Cities = new ObservableCollection<city>();
Cities.Add(new City() { CityName = "Tokyo" });
Cities.Add(new City() { CityName = "Paris" });
Cities.Add(new City() { CityName = "New York City" });
Cities.Add(new City() { CityName = "Singapore" });
Cities.Add(new City() { CityName = "Vienna" });
Cities.Add(new City() { CityName = "Sydney" });
Cities.Add(new City() { CityName = "Zurich" });
Cities.Add(new City() { CityName = "Toronto" });
Cities.Add(new City() { CityName = "Copenhagen" });
}
}
Output:
Conclusion
I hope you enjoyed learning how to create a collection of .NET MAUI CheckBox.
Refer to our .NET MAUI CheckBox’s feature tour page for other groundbreaking feature representations. You can explore our .NET MAUI CheckBox documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI CheckBox and other .NET MAUI components.
Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!