0

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:

  1. Are there reasons to construct my viewmodels with collection properties instead of binding straight to a viewmodel collection class?
  2. If not, how would I bind to my collection inherited class? What would the context be? What would the xaml look like?
maplemale
  • 2,006
  • 21
  • 35
  • 1
    if your BindingContext is a collection, then try ItemsSource="{Binding .}" – Jason Oct 27 '17 at 03:31
  • 2.) `ItemsSource="{Binding}"` which basically means bind to the current BindingContext – Nkosi Oct 27 '17 at 03:31
  • @Nkosi ok, wow. So #2 was a dumb question. ha! Any thoughts on the advantages / disadvantages of using a collection derived class instead of collection properties like I see in most examples? My understanding is that by deriving from the class I'm going to automatically get collection changed notifications from the base? – maplemale Oct 27 '17 at 03:38
  • @maplemale that is how the class `ObservableCollection` operates. It is purely up to you, which makes #1 primarily opinion based. – Nkosi Oct 27 '17 at 03:39
  • @Nkosi Ok, cool. Thank you! Well if you want to add those 2 things as an answer I'll mark as accepted. – maplemale Oct 27 '17 at 03:41
  • @Nkosi Maybe #1 is more appropriately reviewed by this: https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt?rq=1 Though this is more about lists than collections, I think the general points are still applicable? – maplemale Oct 27 '17 at 03:47
  • @maplemale again that is what makes the question opinion based. They both work but it depends on your preference. – Nkosi Oct 27 '17 at 03:55

0 Answers0