【发布时间】:2018-05-03 18:23:49
【问题描述】:
我正在使用 XF 列表视图,并且想绑定整个对象,然后使用转换器根据某些条件格式化数据,但是对象绑定在 XF 中不再起作用。测试在 XF 2.5.0.91635 和 android 支持库 26.1.0.1 上。首先,当不使用项目模板时,列表视图会正确呈现,尽管内容显示的是默认对象字符串:
<ListView x:Name="lv"></ListView>
但是当使用项目模板时,没有显示任何内容,尽管项目的数量确实添加到了列表视图中,并且项目选择事件被正确触发。此外,调试输出以下消息:
<ListView x:Name="lv">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding}"></Label>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
当我将转换器添加到绑定表达式时,转换函数始终在其参数中接收 null。
添加绑定代码:
List<DataItem> list = new List<DataItem>();
for(int i = 0; i < 10; i++)
{
list.Add(new DataItem
{
N = "L " + i.ToString(),
C = i + 1,
});
}
lv.ItemsSource = list;
【问题讨论】:
-
不完全确定发生了什么,但我是否正确理解你这样定义它
<Label Text="{Binding}"></Label>?通常你会像<Label Text="{Binding PropertyOnModel}"></Label>这样定义它,其中PropertyOnModel是数据绑定模型上的一个属性,或者至少<Label Text="{Binding .}"></Label>绑定到整个模型对象。这能解决问题吗? -
是的,绑定到属性有效,但我故意想绑定到整个对象并将其传递给转换器,但它不再起作用,
{Binding .}或{Binding Path=.}的问题相同.
标签: c# xaml data-binding xamarin.forms xamarin.android