Reputation: 390
I have an ObservableCollection of Stylist that I want to display in my view using a CollectionView
this is the code for collectionview
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:Appointments.ViewModels"
x:DataType="viewmodels:WallViewModel"
x:Class="Appointments.Views.WallPage">
<ContentPage.BindingContext>
<viewmodels:WallViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<FlexLayout
JustifyContent="SpaceBetween"
Margin="10, 20">
<Entry
WidthRequest="250"
Placeholder="search.."/>
<Button
Text="Filters"
Command="{Binding OpenFilterCommand}"
/>
</FlexLayout>
<CollectionView
x:Name="StylistList"
BackgroundColor="Transparent"
ItemsSource="{Binding Stylists}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
The Stylist model inherits from a User model that has a public property "Name"
public string Name { get; set; }
but if I run this code it will throw an error saying
"Binding: Propery "Name" not found on WallViewModel"
but if I change the that label to bind to
<Label Text="{Binding .}"/>
and while the app is running I change it back to
<Label Text="{Binding Name}"/>
it will work fine and display all the names inside the collection
Upvotes: 1
Views: 140
Reputation: 1
Just put the iterated type inside the DataTemplate:
<DataTemplate x:DataType="viewmodels:xxx">
...
</DataTemplate>
Upvotes: 0
Reputation: 10938
Due to you set the code below into a User model, when you use x:DataType="viewmodels:WallViewModel"
would thrown the error Property "Name" not found on "App8.WallViewModel".
.
public string Name { get; set; }
If you change the use the x:DataType, you nned to use the class which include the Name property.
For more details about the x:DataType, please check the MS docs. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data- binding/compiled-bindings
As Jason said, remove the x:DataType="viewmodels:WallViewModel"
would fix the this error.
After removing the x:DataType="viewmodels:WallViewModel
, <Label Text="{Binding Name}"/>
should work.
Upvotes: 1