Read the previous parts of the series here,
TableView is used to display the group of the items in Xamarin forms. TableView contains only the cell. To each cell, we can assign a different type of the cell format.
TableView structure is given below:
Note: TableView does not contain the row and column relationship.
Each cell is added into the TableSection and TableSection is added into the TableRoot. Finally TableRoot is added into the TableView.
Cell is a built in template. It represents a different control. Built in templates are available, which are given below:
- Entry Cell
- Switch Cell
- Text Cell
- Image Cell
TableSection
Table section is differentiating different sections like in the form, it differentiates the personal information and educational information.
Properties
- Title
- BindingContext
- RowHeight
Title properties are used to set the title of the section.
RowHeight: It sets the row heights of the property.
Table Root: This is root node for the tableview. All the tablesections must be added into the TableRoot.
Properties
- Title
- BindingContext
Title: This property sets the overall title of the TableView.
Sample code
- <TableView>
- <TableRoot Title="Xamarin TableView">
- <TableSection Title="Section1">
- <SwitchCell Text="Do you like this" /> </TableSection>
- <TableSection Title="Section2">
- <SwitchCell Text="Perfect C#" /> </TableSection>
- </TableRoot>
- </TableView>
Note: Title property is set for only Universal Windows Program.
Intent: Intent property is used to hint at the purpose of the TableView. This property is optional.
Types,
Settings: A table contains the switch and other configuration settings controls.
Data: Display similar items (Xamarin is highly recommended. Instead of the data, use the list view. Later, we will see listview).
Form: It contains forms of data like Registration form.
Menu: Table is used for the selection of items.
Note: TableView does not contain the itemSource element.
SwitchCell:This control is used to enable or disable the option
Properties
On: This property indicates true or false value.
Text: Sets the caption of the control.
OnChange: This event will trigger whenever the enable or disable option is done.
Note: Default installation of Xamarin OnChange event will not work in UWP programming. You have to update Xamarin Forms.
NuGet Package Manager update.
Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for the solution.
Select the Xamain.Forms -> Click Install update.
Example
This example enables “Are you postgraduate.” It loads PG course in the Picker control and disables when it loads the UG course information.
- private void CellEnable_OnOnChanged(object sender, ToggledEventArgs e)
- {
- LoadCourseInfo(TableSection.BEnalble);
- }
- private void LoadCourseInfo(bool loadPg) {
- PickerCourse.Items.Clear();
- if (loadPg) {
- PickerCourse.Title = "PG Course";
- PickerCourse.Items.Add("M.Tech");
- PickerCourse.Items.Add("MCA");
- PickerCourse.Items.Add("MBA");
- } else {
- PickerCourse.Title = "UG Course";
- PickerCourse.Items.Add("B.Tech");
- PickerCourse.Items.Add("BE");
- PickerCourse.Items.Add("BSc");
- }
- PickerCourse.SelectedIndex = 0;
- }
Complete source is available here
GitHub.