0

I'm having a bit of a problem with Xamarin Data Binding. When binding to an item in a C# class, I get the error seen in the title. I've been trying to bind an element from the table example found here. I tried adding BindingContext = this; to the constructor and to change the line above the class declaration to [DesignTimeVisible(false)] the way it is in the NewItemPage from the Master-Detail template found during the creation of the project, however, it did not help.

Here is the code:

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="NetworkScanner.Views.TestPage">
    <TableView>
        <TableRoot>
            <TableSection Title="Getting Started" BindingContext="{x:Reference Table}">
                <ViewCell>
                    <StackLayout Orientation="Horizontal">
                        <Image Source="bulb.png" />
                        <Label Text="left"
                                 TextColor="#f35e20" />
                        <Label Text="right"
                                 HorizontalOptions="EndAndExpand"
                                 TextColor="#503026" />
                    </StackLayout>
                </ViewCell>
            </TableSection>
        </TableRoot>
    </TableView>
</ContentPage>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace NetworkScanner.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    // [DesignTimeVisible(false)]
    public partial class TestPage : ContentPage
    {
        public TableSection Table { get; set; }
        public TestPage()
        {
            InitializeComponent();
            // BindingContext = this;
        }
    }
}
1
  • what are you trying to accomplish? You are setting the BindingContext, but do not have any binding expressions, and you never initialize your Table property. Generally if you want a table that is data driven you would use a ListView or CollectionVIew, not a TableView.
    – Jason
    Jun 20, 2020 at 16:12

1 Answer 1

1

You have to use the keyword Binding instead of x:Reference.

<TableSection Title="Getting Started" BindingContext="{Binding Table}">
3
  • It doesn't result in a build error any more, but the value of Table is still null. Jun 20, 2020 at 15:53
  • 1
    Its null because you have to Create instance of TableSection.
    – Ali Ahmad
    Jun 20, 2020 at 16:17
  • 1
    In constructor create instance like "Table = new TableSection();" or if you don't want to create it give x:Name property to TableSection in your XAML or in your cs file give this name to Table property. <TableSection x:Name="section"> in XAML Table = section; in backend.
    – Ali Ahmad
    Jun 20, 2020 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.