Prettygeek
Prettygeek

Reputation: 2531

Xamarin Forms ListView Data Binding nested objects

I have simple Page with ListView

<ListView x:Name="ForecastView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding mainData.Temperature}" />
        </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>

I'm trying to bind nested property by using . to access it. My item source:

private ObservableCollection<ForecastData> forecast = new ObservableCollection<ForecastData>();

I'm setting it in constructor:

ForecastView.ItemsSource = forecast;

My model is looking like this:

public class ForecastData
    {
        public MainData mainData;
.....
public class MainData
    {
        public double Temperature;
...

After REST call my list is populated by elements (I can select them), but text property is blank. Can You help me figure out what is wrong. I have tried everything and nothing helps (I have read all similar question on Stack Overflow).

Upvotes: 4

Views: 3238

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 33993

The problem is that you are trying to bind to a public field.

You can only bind to properties.

So change:

public MainData mainData;

To:

public MainData mainData { get; set; }

And it should work!

Also for Temperature of course.

Upvotes: 7

Related Questions