Xamarin Forms: Binding: Property "" Not Found on ""

Mike Grainger 21 Reputation points
2021-01-28T13:44:59.377+00:00

Good day: I am having issues getting binding on a collectionview datatemplate.

<ContentPage
    x:Class="MyBloodSugar.Views.ReadingsPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:viewmodel="clr-namespace:MyBloodSugar.ViewModels"
    x:DataType="viewmodel:ReadingsViewModel">

    <ContentPage.BindingContext>
        <viewmodel:ReadingsViewModel />
    </ContentPage.BindingContext>

    <StackLayout>
        <CollectionView ItemsSource="{Binding Readings}" SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid
                        Padding="10"
                        ColumnDefinitions="Auto,*"
                        RowDefinitions="Auto, *">

                        <Label
                            Grid.Column="1"
                            FontAttributes="Bold"
                            Text="{Binding TimeOfDay}" />
                        <Label
                            Grid.Row="1"
                            Grid.Column="1"
                            FontAttributes="Bold"
                            Text="{Binding Weight}" />
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage>

In my viewmodel

 public ObservableRangeCollection<MyReading> Readings { get; private set; }

My Model

public class MyReading
    {
        public DateTime TimeOfDay { get; set; }
        public float Glocouse { get; set; }
        public int Dia { get; set; }
        public int Sys { get; set; }
        public float Weight { get; set; }

        public override string ToString()
        {
            return Glocouse.ToString();
        }

    }

Can you help me understand what I am doing wrong?

Thanks in advance.

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-01-29T02:13:32.297+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    ObservableRangeCollection is a thrid-part nuget-packages, I do not use it, I use System.Collections.ObjectModel.ObservableCollection to replace it, Here is running screenshot, I can get running normally without error.

    61653-image.png

    Here is my ReadingsViewModel.cs.

       using System.Collections.ObjectModel;  
       using System.Text;  
         
       namespace App26  
       {  
           public  class ReadingsViewModel  
           {  
               public ObservableCollection<MyReading> Readings { get; private set; }  
         
                
               public ReadingsViewModel()  
               {  
                   Readings = new ObservableCollection<MyReading>();  
         
                   Readings.Add(new MyReading() { Dia=  1, Sys=2,   Weight=0.2f, Glocouse=0.3f, TimeOfDay=DateTime.Now });  
                   Readings.Add(new MyReading() { Dia = 2, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });  
                   Readings.Add(new MyReading() { Dia = 3, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });  
                   Readings.Add(new MyReading() { Dia = 4, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });  
                   Readings.Add(new MyReading() { Dia = 5, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });  
               }  
         
         
           }  
         
           public class MyReading  
           {  
               public DateTime TimeOfDay { get; set; }  
               public float Glocouse { get; set; }  
               public int Dia { get; set; }  
               public int Sys { get; set; }  
               public float Weight { get; set; }  
         
               public override string ToString()  
               {  
                   return Glocouse.ToString();  
               }  
         
           }  
       }  
    

    I do not use x:DataType="viewmodel:ReadingsViewModel" in your layout.

       <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodel="clr-namespace:App26"  
                    x:Class="App26.Page1">  
           <ContentPage.BindingContext>  
               <viewmodel:ReadingsViewModel />  
           </ContentPage.BindingContext>  
         
           <StackLayout>  
               <CollectionView ItemsSource="{Binding Readings}" SelectionMode="Single">  
                   <CollectionView.ItemTemplate>  
                       <DataTemplate>  
                           <Grid  
                                Padding="10"  
                                ColumnDefinitions="Auto,*"  
                                RowDefinitions="Auto, *">  
         
                               <Label  
                                    Grid.Column="1"  
                                    FontAttributes="Bold"  
                                    Text="{Binding TimeOfDay}" />  
                               <Label  
                                    Grid.Row="1"  
                                    Grid.Column="1"  
                                    FontAttributes="Bold"  
                                    Text="{Binding Weight}" />  
                           </Grid>  
                       </DataTemplate>  
                   </CollectionView.ItemTemplate>  
               </CollectionView>  
           </StackLayout>  
       </ContentPage>  
    

    Best Regards,

    Leon Lu


    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. mhe Garage 11 Reputation points
    2022-04-09T10:15:15.62+00:00

    Hi
    I know its already commented as an answer but I would like to add my 50 cent

    <CollectionView ItemsSource="{Binding Readings}" SelectionMode="Single">
                 <CollectionView.ItemTemplate>
                     <DataTemplate>
    

    in DataTemplate you should have to define the data type so the compiler know where to find the items

    like this :

    <DataTemplate x:DataType="viewmodels:Emptydesck">
                    <ImageCell Text="{Binding IDEmptydesck}"
                               Detail="{Binding roomNu}" />
    
                </DataTemplate>
    

    while viewmodels:Emptydesck is refer to

     public class Emptydesck
        {
            public string IDEmptydesck { get; set; }
            public string roomNu { get; set; }
        }
    
    2 people found this answer helpful.
    0 comments No comments

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.