Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

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

$
0
0

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; }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles