I've created a ListView with a Binding to an Itemssource, as well as a DataTemplate with ViewCells to output values of the underlying items.
But what's happening is that the ViewCell seems to have the BindingContext of the ListView set instead of the items. This is a bit hard to debug due to the nature of xaml... does anyone ever had a similar problem?
Here is my Xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LocationTracker2.MyPage"
xmlns:localVm="clr-namespace:LocationTracker2.ViewModel;assembly=LocationTracker2">
<ContentPage.Content>
<StackLayout>
<Button Text="Start Location tracking" Command="{Binding StartTrackingCommand}"/>
<Button Text="Stop Location tracking" Command="{Binding StopTrackingCommand}"/>
<Label Text="{Binding Status}"/>
<ListView ItemsSource="{Binding Entries}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Timestamp}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
And this is my VM:
`public class MyPageVm : INotifyPropertyChanged
{
private ObservableCollection _entries = new ObservableCollection ();
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<LogEntry> Entries {
get { return _entries; }
}
}`
The LogEntry:
public class LogEntry
{
public DateTime Timestamp {get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
and this is where i create the view and set the bindingcontext:
public static Page GetMainPage ()
{
if (_mainPage == null) {
var vm = new MyPageVm ();
_mainPage = new MyPage ();
_mainPage.BindingContext = vm;
_vm = vm;
}
return _mainPage;
}
Thanks for any help, seems i'm really stuck there...