Hi,
I have a problem when I try to set ListView ItemsSource and DataTemplate via c# code. When I define my Page in Xaml, everything works fine.
My Model:
`public class ListItemValue {
private string _name;
public string Name {
get {
return _name;
}
set {
_name = value;
}
}
}
`
My ViewModel (BindingContext of Page):
...
public ObservableCollection<ListItemValue> StringDataSource { get; set; }
...
My XAML page definition where everything is working:
...
<ListView x:Name="myListView" ItemsSource="{Binding StringDataSource}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
...
My XAML Page deifnition where I have troubles with:
<ListView x:Name="myListView" />
Setting the following with code:
myListView.SetBinding(ListView.ItemsSourceProperty, "StringDataSource");
var template = new DataTemplate(typeof(TextCell));
template.SetBinding(TextCell.TextProperty, "Name");
myListView.ItemTemplate = template;
The working example looks like this in my emulator: http://i.imgur.com/rNSDxEA.png
The problematic one looks like this: http://i.imgur.com/LguYe7Z.png
So ... what I am trying to solve is that the DataBinding inside of the ListView is not working correctly ... Actually im just seeing Object.ToString() as text.
I've also tried to modify the non working XAML definition to this, just setting the ItemSource via c# without the DataTemplate. But sadly same results.
<ListView x:Name="myListView">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
What else can I do? What am I doing wrong? Xamarin.Forms 1.3.1.6296, Android