Help with ListView HeaderTemplate Bindings.

LesterMeeksLesterMeeks USMember ✭✭
edited January 2018 in Xamarin.Forms

I feel like I am just missing something stupid, if you can point it out that would help me immensly.

I am trying to show a ListView with Grouping enabled and using the HeaderTemplate with a binding. This is my very simple ItemSource classes. The MyDataItem bindings are all working, and the items show up just fine. The header labels show up but the Text shown on them is the fully qualified class name (as if the label was picking up the ToString) output of ListSection. Everything is working except the Bindings/Values that I am trying to use in the HeaderTemplate. I have looked around at the forums, and it seems that the "Header" binding is supposed to be there, but I don't see any changes if I set a root binding or not to the "Header". If I use the GroupDisplayBinding, things work great but obviously it shows the stock group header. Any thoughts?

            m_dataList = new ListView(ListViewCachingStrategy.RetainElement)
            {
                ItemsSource = m_listRoot,
                HasUnevenRows = true,
                ItemTemplate = new CellDataTemplateSelector(),
                Header = new Binding("."),
                HeaderTemplate = new DataTemplate(typeof(Label)){
                    Bindings = {
                        //{Label.TextProperty, new Binding("Name")},
                    },
                    Values = {
                        { Label.TextProperty, "Test" },
                        { Label.BackgroundColorProperty, Color.Red }
                    }

                },

                SeparatorVisibility = SeparatorVisibility.None,
                SeparatorColor = Color.Transparent,
                BackgroundColor = ColorPalette.GroupHeaderBackground,
                IsGroupingEnabled = true,

                //GroupDisplayBinding = new Binding("Name"),
                //GroupShortNameBinding = new Binding("Name"),
            };


    private class ListSection : ObservableCollection<ReadingDataItem>
    {

        public string Name { get; private set; }

        public ListSection(string name)
        {
            this.Name = name;
        }
    }

    private class ListRoot : ObservableCollection<ListSection>
    {

    }

Best Answer

  • LesterMeeksLesterMeeks US ✭✭
    Accepted Answer

    Dang it... Turns out if you use the right property "GroupHeaderTemplate" things work better. Header is a header for the whole list view, GroupHeader is for the group. ;)

Answers

  • LesterMeeksLesterMeeks USMember ✭✭
    Accepted Answer

    Dang it... Turns out if you use the right property "GroupHeaderTemplate" things work better. Header is a header for the whole list view, GroupHeader is for the group. ;)

Sign In or Register to comment.