Binding: Property not found

Michael Felleisen 146 Reputation points
2021-03-17T16:03:41.783+00:00

Hi,

Complete beginner here!
I am trying to create a simple ListView using Mvvmhelpers and I am having trouble with data binding.

.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:model="clr-namespace:Test.Models"
             xmlns:viewmodels="clr-namespace:Test.ViewModels"

             x:Class="Test.Views.DataContainerPage"
             x:DataType="viewmodels:DataContainerViewModel">

    <ContentPage.BindingContext>
        <viewmodels:DataContainerViewModel/>
    </ContentPage.BindingContext>

    <ListView CachingStrategy="RecycleElement"
              ItemsSource="{Binding DataContainers}">

        <ListView.ItemTemplate>
            <DataTemplate x:DataType="model:DataContainer">
                <ViewCell>
                    <Grid>
                        <Label Text="{Binding ID}"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>
</ContentPage>

xaml.cs

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

namespace Test.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DataContainerPage : ContentPage
    {
        public DataContainerPage()
        {
            InitializeComponent();
        }
    }
}

Class I want to use in my DataTemplate

        namespace Test.Models
        {
            public class DataContainer
            {
                public string ID;
            }
        }

And finally my viewmodel class:

using MvvmHelpers;
    using Test.Models;

    namespace Test.ViewModels
    {
        public class DataContainerViewModel : BaseViewModel
        {
            public ObservableRangeCollection<DataContainer> DataContainers { get; set; }

            public DataContainerViewModel()
            {
                DataContainers = new ObservableRangeCollection<DataContainer>();

                DataContainers.Add(new DataContainer { ID = "0" });
                DataContainers.Add(new DataContainer { ID = "1" });
                DataContainers.Add(new DataContainer { ID = "2" });
            }
        }
    }

If I try to bind the label text to the DataContainer.ID visual studio auto-completes ID for me, so something is working here.
But if I try to run the app I get the builderror:
Binding: Property "ID" not found on "Test.Models.DataContainer".
Do you guys have any idea why this is failing in this case? I have code where this kind of binding works .. in the same project.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,372 questions
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,711 Reputation points Microsoft Vendor
    2021-03-18T02:50:25.067+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    I made a demo according to your code, it works properly. The code is as follows,

    1.class DataContainer

    public class DataContainer  
    {  
        public string ID { get; set; }  
    }  
    

    2.class DataContainerViewModel

     public class DataContainerViewModel  
    {  
        public ObservableCollection<DataContainer> DataContainers { get; set; }  
    
        public DataContainerViewModel()  
        {  
            DataContainers = new ObservableCollection<DataContainer>();  
    
            DataContainers.Add(new DataContainer { ID = "0" });  
            DataContainers.Add(new DataContainer { ID = "1" });  
            DataContainers.Add(new DataContainer { ID = "2" });  
        }  
    }  
    

    3.MainPage.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:formapp1="clr-namespace:FormApp1"  
                 x:Class="FormApp1.MainPage">  
    
    
        <ContentPage.BindingContext>  
            <formapp1:DataContainerViewModel></formapp1:DataContainerViewModel>  
        </ContentPage.BindingContext>  
    
        <StackLayout>  
            <ListView CachingStrategy="RecycleElement"  
                   ItemsSource="{Binding DataContainers}">  
                <ListView.ItemTemplate>  
                    <DataTemplate >  
                        <ViewCell>  
                            <Grid>  
                                 <Label Text="{Binding ID} " TextColor="Red" HorizontalOptions="Center" FontSize="Large"/>  
                            </Grid>  
                        </ViewCell>  
                    </DataTemplate>  
                </ListView.ItemTemplate>  
            </ListView>  
        </StackLayout>  
    </ContentPage>  
    

    Note:

    1. For ID in class DataContainer, add { get; set; } ;

    2.We don't need add x:DataType in xaml file for our page;(You can check above code);
    3.To make the UI look better, I use the following code :

    <Label Text="{Binding ID} " TextColor="Red" HorizontalOptions="Center" FontSize="Large"/>  
    

    The result is:
    78974-image.png

    For more details, you can refer the official document : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.