Table View in Xamarin - Forms

How to display list data into table view in Xamarin.forms?

Tagged:

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    TableView does not have the concept of an ItemsSource, so items must be manually added as children. You can add data to the TableView by traversing the list. Refer to the code:
    page.xaml.cs

    public partial class Page2 : ContentPage
    {
        ObservableCollection<_model> list = new ObservableCollection<_model>();
        public Page2()
        {
            InitializeComponent();
    
            list.Add(new _model { content = "label_1" });
            list.Add(new _model { content = "button_2" });
            foreach (var item in list)
            {
                layout.Children.Add(new Label() { Text = item.content, HorizontalOptions = LayoutOptions.CenterAndExpand });
            }
        }
    }
    

    page.xaml

    <TableView>
        <TableRoot>
            <TableSection x:Name="section">
                <ViewCell>
                    <StackLayout x:Name="layout" Orientation="Vertical">
                    </StackLayout>
                </ViewCell>
            </TableSection>
        </TableRoot>
    </TableView>
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Or just use a ListView...

Sign In or Register to comment.