XF MasterDetailPage - MasterPage with ListView wont bind to ViewModel (MVVM)

jaspalsinghkhokharjaspalsinghkhokhar GBMember ✭✭
edited January 2016 in Xamarin.Forms

Hi all,

I am just starting out with XF so I apologise in advance if this seems trivial!

I have set up a master detail page and have used the code behind to populate the ListView in my master page. This all works fine.

Now moving the code behind into a ViewModel the application works but the ListView does not populate.

Any help would be appreciated.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"





         Title="SMDC">  <ContentPage.Content>

<StackLayout VerticalOptions="FillAndExpand">

  <ListView VerticalOptions="FillAndExpand" ItemsSource="{Binding GetMainMenuListItems}">



        <ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />




</StackLayout>  </ContentPage.Content></ContentPage>`

public class MainMenuMasterViewModel { // var mainMenuViewItems = new List<MainMenuItem>(); private List<MainMenuItem> mainMenuListItems; public MainMenuMasterViewModel() { LoadData(); } public List<MainMenuItem> GetMainMenuListItems { get { return mainMenuListItems; } set { mainMenuListItems = value; } } private void LoadData() { mainMenuListItems.Add(new MainMenuItem { Title = "Assets", IconSource = "assets.png", TargetType = typeof(AssetsView) }); mainMenuListItems.Add(new MainMenuItem { Title = "Data Transfer", IconSource = "dataTransfer.png", TargetType = typeof(DataTransferView) }); }


public class MainMenuItem { public string Title { get; set; } public string IconSource { get; set; } public Type TargetType { get; set; } }


  • jaspalsinghkhokharjaspalsinghkhokhar GBMember ✭✭
    edited January 2016


  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    It appears to me that you have not actually assigned the ViewModel to be the BindingContext of your page.

    I don't use XAML that often, so I cannot remember how to do it there, but someone else should be able to point you to that if as I suspect you have not done so.

    If no one supplies it, what you can do is in the code behind:

    public partial class YourContentPage : ContentPage
        BindingContext = new YourVMClass();
  • jaspalsinghkhokharjaspalsinghkhokhar GBMember ✭✭
    edited January 2016

    thanks, overlooked this, now seems to be running as my breakpoint in the constructor is now being hit.

    in xaml

Sign In or Register to comment.