I am writing a forecast weather app and i cant bind property of object in ListView.
Collection consists of WeatherModel objects, which have string property "Temperature"
Xaml sees the collection and outputs it, but cant find the object property "Temperature".
How to bind property "Temperature"?
This is my 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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="IMAP.View.Weather"
xmlns:local="clr-namespace:IMap.ViewModel;assembly=IMAP">
<ContentPage.Resources>
<local:ForecastWeatherViewModel x:Key="vm"/>
</ContentPage.Resources>
<ContentPage.Content>
<ListView ItemsSource="{Binding items, Source={StaticResource vm}}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="2,15">
<Label Text="{Binding Temperature}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
This is my ViewModel
namespace IMap.ViewModel
{
public class ForecastWeatherViewModel : ViewModel
{
public ObservableCollection<WeatherModel> WeatherList { get; set; }
public ObservableCollection<WeatherModel> items
{
get
{
return WeatherList;
}
}
public ForecastWeatherViewModel()
{
WeatherList = new ObservableCollection<WeatherModel>();
WeatherList.Add(new WeatherModel()
{
Temperature = SomeTemp
});
WeatherList.Add(new WeatherModel()
{
Temperature = SomeTemp
});
OnPropertyChange();
}
}
}
This is my Model
namespace IMAP.Model
{
public class WeatherModel
{
public string Temperature;
}
}
Try replacing your Temperature
variable with a property like such:
namespace IMAP.Model
{
public class WeatherModel
{
public string Temperature { get; set; }
}
}