GroupHeaderTemplate is created twice for single group

3 posts, 0 answers
  1. VincentH
    VincentH avatar
    8 posts
    Member since:
    Sep 2016

    Posted 28 Oct Link to this post

    Hi,

    I am trying to create the equivalent of a header template in the RadListView by using grouping with a single group.

    The problem is that the group header template is invoked twice instead of once, resulting in 2 headers displayed on top of each other with a slight offset. Am I doing something wrong or is this a bug in RadListView?

    Here are my relevant code snippets:

    list.GroupHeaderTemplate = new DataTemplate(() => {
        return new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "Welcome to Xamarin Forms!" }; // This is invoked twice instead of once
    });

    list.GroupDescriptors.Add(new PropertyGroupDescriptor { PropertyName = nameof(TestItem.GroupKey) });

    Where the ItemsSource is a List<TestItem> with 50 items and the TestItem class is defined as:

        class TestItem
        {
            static object groupKey = new object();
            public object GroupKey { get { return groupKey; } }
        }


  2. VincentH
    VincentH avatar
    8 posts
    Member since:
    Sep 2016

    Posted 31 Oct in reply to VincentH Link to this post

    PS When I create a single header instance and return that each time in the GroupHeaderTemplate (see code below), the header looks OK but I still wonder whether this invoke twice behaviour is by design or a bug?

    var header = new Label { HorizontalTextAlignment = TextAlignment.Center, Text = "Welcome to Xamarin Forms!" };
    GroupHeaderTemplate = new DataTemplate(() => {
      return header; // This is called twice instead of once so do not create new view instances here
    });

  3. DevCraft banner
  4. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 01 Nov Link to this post

    Hello Vincent,

    I tried to reproduce the issue on our side and it seems that the code works as expected. I tested this scenario with the following code:

        public StartPage()
        {
            InitializeComponent();
            var list = new RadListView();
            list.ItemsSource = this.GenerateSource();
            list.GroupDescriptors.Add(new PropertyGroupDescriptor() { PropertyName = "Day" });
            list.ItemTemplate = new DataTemplate(() =>
            {
                return new ListViewTemplateCell()
                {
                    View = CreateView()
                };
            });
            list.GroupHeaderTemplate = new DataTemplate(() =>
            {
                return new Label()
                {
                    HorizontalTextAlignment = TextAlignment.Center,
                    Text = "Welcome to Xamarin Forms!"
                };
            });
            Content = list;
        }
        private View CreateView()
        {
            var b = new Binding() { Path = "Content" };
            var l = new Label();
            l.SetBinding(Label.TextProperty, b);
            var g = new Grid();
            g.Children.Add(l);
            return g;
        }
        private System.Collections.IEnumerable GenerateSource()
        {
            var results = new List<Event>();
            results.Add(new Event() { Content = "You are now free to do whathever", Day = "Today" });
            results.Add(new Event() { Content = "Content of the item", Day = "Today" });
            results.Add(new Event() { Content = "This also happens today", Day = "Today" });
            results.Add(new Event() { Content = "More events today", Day = "Today" });
            results.Add(new Event() { Content = "Go shopping after 19:00", Day = "Today" });
            return results;
        }
    }
    internal class Event
    {
        public string Content { get; set; }
        public string Day { get; set; }
    }
    Could you please share more information about your setup? Which version of the controls you use? Is your setup different in any way? Could you please try to reproduce the reported behavior in a small repro project and send it over.  It will allow us to further investigate the reasons behind.

    Regards,
    Pavel R. Pavlov
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top