My only experiences with Databinding in xaml come from a Silverlight and RIA background, which may be the reason for my confusion. I may design a viewmodel as follows:
public class ViewModelMyVehicles : ObservableCollection<ViewModelMyVehicle>
{
public ViewModelMyVehicles()
{
using (App.dbConn)
{
var results = App.dbConn.Query<ViewModelMyVehicle>("SELECT *
FROM MyVehicles;");
foreach (ViewModelMyVehicle r in results)
{
this.Add(r);
}
}
}
}
Notice, how I inherit from ObservableCollection. In Xamarin, if I do this, I cannot seem to figure out the binding. Normally, I'm setting the binding context as a DomainDatasource (silverlight) and ItemSource to the collection class itself (instead of a collection property). Here are the examples I see everyone using in Xamarin:
public class ViewModelMyVehicles
{
public ViewModelMyVehicles()
{
myVehicles = new ObservableCollection<ViewModelMyVehicle>();
using (App.dbConn)
{
var results = App.dbConn.Query<ViewModelMyVehicle>("SELECT *
FROM MyVehicles;");
foreach (ViewModelMyVehicle r in results)
{
myVehicles.Add(r);
}
}
}
public ObservableCollection<ViewModelMyVehicle> myVehicles { get; set; }
}
If I do that, the binding works out like this:
<ContentPage.BindingContext>
<local:ViewModelMyVehicles/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout Orientation="Vertical">
<StackLayout Padding="15" Orientation="Horizontal">
<Label Text="My Garage: " />
<Picker x:Name="pickerMyVehicles" ItemsSource="{Binding myVehicles}" ItemDisplayBinding="{Binding Name}" SelectedIndex="1" />
</StackLayout>
</StackLayout>
</ContentPage.Content>
I guess my question is two fold:
- Are there reasons to construct my viewmodels with collection properties instead of binding straight to a viewmodel collection class?
- If not, how would I bind to my collection inherited class? What would the context be? What would the xaml look like?