UWP GroupHeader isn't rendered when using ListViewRenderer

NestoriumNestorium USMember
edited September 2016 in Xamarin.Forms

I have the following HeaderTemplate defined in App.xaml:
<DataTemplate x:Key="ListViewGroupHeaderTemplate"> <TextBlock Foreground="Black" Text="Something" /> </DataTemplate>
I use it this way in my ListViewRenderer:
listView.ItemTemplate = App.Current.Resources["ListViewItemTemplate"] as Windows.UI.Xaml.DataTemplate; var groupStyle = App.Current.Resources["groupStyle"] as GroupStyle; listView.GroupStyle.Add(groupStyle);
The source of my ListView is a CollectionViewSource:
var source = new ObservableCollection<GroupItem>(); for (int i = 0; i < 10; i++) { var s = new GroupItem("Header " + i, "ShortName " + i); for (int j = 0; j < 6; j++) { s.Add(new ListItem { Value = j, Text = "Text " + j }); } source.Add(s); } CollectionViewSource cvs = new CollectionViewSource(); cvs.IsSourceGrouped = true; List<ListItem> list = new List<ListItem>(); foreach (var item in source) { list.AddRange(item); } var group = from item in list group item by item.Value into grp orderby grp.Key select grp; cvs.Source = group;

I've created a sample project and if I don't use the ListViewRenderer, I get this:

When I use it, I get this:

None of the group headers is rendered.

You can download the sample projects (working and not working) from here.

Can you tell me what is wrong with the ListViewRenderer?

Posts

  • KrisPetkovKrisPetkov BGMember ✭✭

    Hello,

    I'am facing the same problem here and cannot find any solution...
    Did you find something how to bypass this problem?

  • KrisPetkovKrisPetkov BGMember ✭✭
    edited December 2016

    Ok, finally after day and a half of struggling with this problem I found the solution. I'll post it here if someone someday face the same problem as me. Actually it is really strange problem and I don't know if it is a bug, unimplemented feature or what...

    The solution:

    In your UWP custom renderer you need to do this:

    `
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
    {
    base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            e.NewElement.GroupHeaderTemplate = new Xamarin.Forms.DataTemplate(() =>
            {
                var label = new Label();
    
                label.SetBinding(Label.TextProperty, "Country");
    
                return new ViewCell { View = label };
            });
        }
    }
    

    `

    Where "Country" is the name of the property from the class representing your Group/Header.

Sign In or Register to comment.