4

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).

Prettygeek
  • 2,461
  • 3
  • 22
  • 44
  • 1
    "Tamarin forms" – devRicher Dec 06 '16 at 12:40
  • Try to read here: http://stackoverflow.com/questions/36985634/xamarin-forms-databinding-separator, http://stackoverflow.com/questions/31041542/how-to-bind-to-a-nested-class-wpf and http://stackoverflow.com/questions/14546347/how-to-use-nested-class-in-wpf-xaml. – EgoPingvina Dec 06 '16 at 12:49
  • 1
    you can only bind to public properties (with getters and setters) not public variables or methods – Jason Dec 06 '16 at 13:02

1 Answers1

7

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.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100