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.

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

         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:vm="clr-namespace:MasterDetailPageNavigation.ViewModels;assembly=MasterDetailPageNavigation" 

         x:Class="MasterDetailPageNavigation.Views.MainMenuMasterView"

         Padding="0,40,0,0"

         Icon="hamburger.png"

         Title="SMDC">  <ContentPage.Content>

<StackLayout VerticalOptions="FillAndExpand">

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

    <ListView.ItemTemplate>

      <DataTemplate>

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

      </DataTemplate>

    </ListView.ItemTemplate>

  </ListView>

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

MainMenuMasterViewModel.cs
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) }); }

MainMenuItem.cs

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

Posts

  • 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

      xmlns:vm="clr-namespace:MasterDetailPageNavigation.ViewModels;assembly=MasterDetailPageNavigation" 
    
      <ContentPage.BindingContext>
        <vm:MainMenuMasterViewModel/>
      </ContentPage.BindingContext>
    
Sign In or Register to comment.