This is a migrated thread and some comments may be shown as answers.

DataTemplateSelector for GroupHeaderTemplate

1 Answer 122 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 2
Joshua asked on 08 Apr 2020, 04:45 PM
Is it possible to use a DataTemplateSelector for the GroupHeaderTemplate, much like we can with the ItemTemplate (through ItemTemplateSelector)? I cannot find any documentation on this.

1 Answer, 1 is accepted

Sort by
Lance | Manager Technical Support
Telerik team
answered on 08 Apr 2020, 07:28 PM

Hi Joshua,

The reason there isn't a DataTemplateSelector for the GroupHeaderTemplate is because the BindingContext of the GroupHeader is always the same, there's nothing to select on (i.e. it will always be an instance of type Telerik.XamarinForms.DataControls.ListView.GroupHeaderContext).

Customize Header Based on Group

That being said, I'm assuming you're ultimately looking for a way to customize the contents of the template based on different values for the group Key.

This can be done with other styling approaches like converters or DataTriggers because the GroupHeaderContext gives more than just the text you see in the header (that's the Key.ToString()).

You can read the full descriptions of those properties in the GroupHeaderTemplate documentation.


I've written you an example project to help you get started creating your own custom headers. The demo that does two things:

  • Changes the background color of the GroupHeader based on IsExpanded (with a DataTrigger)
  • Changes the Key's Label TextColor based on the value of the Key (with a converter)

    <local:GroupKeyToColorConverter x:Key="GroupKeyToColorConv"/>

    <DataTemplate x:Key="MyGroupHeaderTemplate">
                <DataTrigger TargetType="Grid"
                             Binding="{Binding IsExpanded}"
                    <Setter Property="BackgroundColor"
                            Value="AntiqueWhite" />
                <DataTrigger TargetType="Grid"
                             Binding="{Binding IsExpanded}"
                    <Setter Property="BackgroundColor"
                            Value="GhostWhite" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            <Label Text="&#x25B8;"
                   Margin="8, 12, 0, 6"
                    <DataTrigger TargetType="Label"
                                 Binding="{Binding IsExpanded}"
                        <Setter Property="Text"
                                Value="&#x25BE;" />
            <Label Margin="0, 12, 0, 6"
                   Text="{Binding Key}"
                   TextColor="{Binding Converter={StaticResource GroupKeyToColorConv}}"
                   HorizontalOptions="Start" />

Here's the result at runtime


If you would like to get further assistance, I recommend opening a support ticket. You get full support during your trial. A Support Ticket guarantees a response from the Telerik UI for Xamarin team within the ticket's time-frame.

We do our best to answer forum threads within a reasonable time-frame, but this is limited by available resources (i.e. current support load) and a forum reply can take a few days because support tickets always come first.

Note: I see you currently have trials, if your employer has a purchased version make sure they assign you as the Developer. This will give you full access to their Support plan and products, but they still own the license. It only takes 20-30 seconds via the Manage Licensed Users page.

Lance | Team Lead - US DevTools Support
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Asked by
Top achievements
Rank 2
Answers by
Lance | Manager Technical Support
Telerik team
Share this question