Reputation: 13846
I'm using XF list view, and want to binding the whole object and then use converter to format the data based on some conditions, but object binding does not work anymore in XF. Test is on XF 2.5.0.91635, and android support library 26.1.0.1. First, when do not use item template, the list view is rendered correctly, though content is showing default object string:
<ListView x:Name="lv"></ListView>
But when using item template, nothing is show, though the number of items are indeed added in list view, and item selected event is fired correctly. Also, debug outputs following messages:
<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'
And when I add converter to binding expression, the convert function always receives null in its arguments.
add binding code:
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;
Upvotes: 1
Views: 357
Reputation: 10841
And when I add converter to binding expression, the convert function always receives null in its arguments.
If you want to bind to entire object you will need to modify the DataItem
like this:
public class DataItem
{
public String N { get; set; }
public int C { get; set; }
//use an Item property to reference itself
public DataItem Item { get { return this; } }
}
Then you can use it in your ListView
together with converter:
<ListView x:Name="lv" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Item,Converter={StaticResource YourConverter}}" ></Label>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Upvotes: 1
Reputation: 10750
Correct way to bind is "Binding collection to ListView" and by providing ItemSource
for list:
For example:
private ObservableCollection<Models.DataItem> _dataList;
public ObservableCollection<Models.DataItem> DataList
{
get { return _dataList; }
set
{
_dataList = value;
RaisePropertyChanged(() => DataList); // you can use your own property notification mechanism here.
}
}
Then after setting binding context to your ViewModel:
<ListView ItemsSource="{Binding DataList}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding . }" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Or if you want to display properties from following DataItem
:
public class Model.DataItem
{
public string Property1 {get; set; }
public string Property2 {get; set; }
public string Property3 {get; set; }
}
use
<ViewCell>
<Label Text="{Binding Property1 }" />
<Label Text="{Binding Property2 }" />
<Label Text="{Binding Property3 }" />
</ViewCell>
Hope this helps
Upvotes: 0