Forum Xamarin.Forms

MasterDetailPage DataTemplate doesn't working

Hello,

I'ved read the documentation today at xamarin-doc and i'ved juste adapt one or more elements. But I have a problem, the text is not visible. The list exist, and she have 2 line (I can click on it), but the text property is empty.

MainPageCS

using System;
using Xamarin.Forms;

namespace GestionnairesCopro.Master.SignIn
{
    public class MainPageCS : MasterDetailPage
    {
        MasterPageCS masterPage;

        public MainPageCS()
        {
            masterPage = new MasterPageCS();
            Master = masterPage;
            Detail = new NavigationPage(new SignInPage());
            masterPage.ListView.ItemSelected += OnItemSelected;
        }

        void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as MasterPageItem;
            if (item != null)
            {
                Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
                masterPage.ListView.SelectedItem = null;
                IsPresented = false;
            }
        }
    }
}

MasterPageCS

using System.Collections.Generic;
using Xamarin.Forms;

namespace GestionnairesCopro.Master.SignIn
{
    public class MasterPageCS : ContentPage
    {
        public ListView ListView { get { return listView; } }

        ListView listView;

        public MasterPageCS()
        {
            var masterPageItems = new List<MasterPageItem>();
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Connexion",
                TargetType = typeof(SignInPage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "ParamËtres",
                TargetType = typeof(SettingsPage)
            });

            listView = new ListView
            {
                ItemsSource = masterPageItems,
                ItemTemplate = new DataTemplate(() => {
                    var textCell = new TextCell();
                    textCell.SetBinding(TextCell.TextProperty, "Title");
                    return textCell;
                }),
                VerticalOptions = LayoutOptions.FillAndExpand,
                SeparatorVisibility = SeparatorVisibility.None
            };

            Padding = new Thickness(0, 40, 0, 0);
            Icon = "hamburger.png";
            Title = "Personal Organiser";
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children = {
                    listView
                }
            };
        }
    }
}

MasterPageItem

using System;

namespace GestionnairesCopro.Master.SignIn
{
    class MasterPageItem
    {
        public string Title;

        public Type TargetType;
    }
}

Sorry for my bad english.
Thanks for your help.

Best Answer

Answers

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    Up. Please, need help.

  • DroideDroide ITMember

    Hi,

    i think i've faced this issue already once. The text is not empty, it's just the color of the TextCells that is probably inheriting from the background color of the MasterPage so you can't read anything.

    Try to set the TextColor property of TextCell items inside the listview, something like this (assuming MasterPage Background color is black):

     listView = new ListView
                {
                    ItemsSource = masterPageItems,
                    ItemTemplate = new DataTemplate(() => {
                        var textCell = new TextCell();
                        textCell.SetBinding(TextCell.TextProperty, "Title");
    
                 textCell.TextColor = Color.White;
                         textCell.DetailColor = Color.Red;
    
                        return textCell;
                    }),
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    SeparatorVisibility = SeparatorVisibility.None
                };
    

    Hope it helps.

  • MaxenceSAUNIERMaxenceSAUNIER USMember ✭✭✭

    Thanks for your help but it's not that :'( I don't understand.

Sign In or Register to comment.