rabibi
rabibi

Reputation: 13

Error my collectionview receives a list in the ItemSource and does not recognize the elements of that list

I have a Person object with properties of Person{ string name; int number}; I built a form to send data to a Persons=list :

<Label Text="Name:"/>
             <Entry x:Name="labelName" Text="{Binding Person.Name}" Placeholder="Name"/>
             <Label Text="Nif:"/>
             <Entry x:Name="labelNumber" Text="{Binding Person.Number}" Placeholder="Number"/>
         <Button Text="Add" Command="{Binding AddCommand}"/>

then I wanted to show this data below in a collectionview but an error does not appear:

<CollectionView ItemsSource="{Binding Persons}">
         <CollectionView.ItemTemplate>
                 <DataTemplate>
                     <Label Text="{Binding Name}"/>
                 </DataTemplate>
         </CollectionView.ItemTemplate>
     </CollectionView>

Error " The Name "Name" don't exist in the current context"

I don't understand why it gives me the error I did everything correctly and by binding

After choosing ItemSsource={Binding Persons} it would be possible that the elements chosen below would be from Person element, that is, it should work <Label Text={Binding Name} but not working.

Upvotes: 0

Views: 158

Answers (1)

Jason
Jason

Reputation: 89129

you need a DataType

xmlns:mynamespace="clr-namespace:MyNameSpaceForMyClass"
...
<DataTemplate x:DataType="mynamespace:Person">

you will need to specify the correct namespace using xmlns

Upvotes: 0

Related Questions