[英]Xamarin Forms ListView Data Binding nested objects
I have simple Page with ListView 我有一个简单的页面与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. 在REST调用之后,我的列表由元素填充(我可以选择它们),但text属性为空。 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).
我已经尝试了一切,没有任何帮助(我已经阅读了Stack Overflow上的所有类似问题)。
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. 当然也适用于
Temperature
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.