No Data Context Found for Binding Recipes

Viewed 68

I'm having a problem on how to display my binding context, i have it on my code behind but its not working. it says no data context found

I tried other methods but it still doesn't read my binding. Here is my xaml code

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="App1.RecipePage1"
            Title="Recipe Name">
    <ContentPage Title="Ingredients">
        <StackLayout>
            <ListView ItemsSource="{Binding Recipes}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <Label Grid.Row="0" Grid.Column="0" Text="{Binding Ingredient}"></Label>
                                    <Label Grid.Row="0" Grid.Column="1" Text="{Binding Quantity}"></Label>
                                </Grid>
                            </Frame>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>

    </ContentPage>

And this is my code behind

    public partial class RecipePage1 : TabbedPage
    {
        public IList<Recipe> Recipes { get; set; }
        public RecipePage1()
        {
            InitializeComponent();
            Recipes = new List<Recipe>();
            Recipes.Add(new Recipe() { Ingredient = "Kimchi", Quantity = "100 Grams" });
            Recipes.Add(new Recipe() { Ingredient = "Noodle", Quantity = "80 Grams" });
            Recipes.Add(new Recipe() { Ingredient = "Scallions", Quantity = "150 Grams" });
            Recipes.Add(new Recipe() { Ingredient = "Spinach", Quantity = "200 Grams" });
            Recipes.Add(new Recipe() { Ingredient = "Prawns", Quantity = "500 Grams" });
            BindingContext = this;
        }
        public class Recipe
        {
            public string Ingredient { get; set; }
            public string Quantity { get; set; }
        }
    }

there is no error, it will run but it doesnt read my binding even if i already called it on my behind

1 Answers

It is a simple binding problem. If you want to use the binding in the xaml, you need to use the MVVM to set a ViewModel which implements the INotifyChanged interface as the binding context. You can check the official document about it.

In addition, you can also give the ListView a name such as:

<ListView x:Name = "listview">

And set the ItemSource in the page such as:

listview.ItemSource = Recipes;

You can check the official document about the ListView.

Related