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

DataTemplateSelector for GroupHeaderTemplate

1 Answer 242 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Joshua
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
1
Accepted
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.

Demo

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)

<ContentPage.Resources>
    <local:GroupKeyToColorConverter x:Key="GroupKeyToColorConv"/>

    <DataTemplate x:Key="MyGroupHeaderTemplate">
        <Grid>
            <Grid.Triggers>
                <DataTrigger TargetType="Grid"
                             Binding="{Binding IsExpanded}"
                             Value="False">
                    <Setter Property="BackgroundColor"
                            Value="AntiqueWhite" />
                </DataTrigger>
                <DataTrigger TargetType="Grid"
                             Binding="{Binding IsExpanded}"
                             Value="True">
                    <Setter Property="BackgroundColor"
                            Value="GhostWhite" />
                </DataTrigger>
            </Grid.Triggers>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Label Text="&#x25B8;"
                   Margin="8, 12, 0, 6"
                   TextColor="DarkGray"
                   FontSize="Medium">
                <Label.Triggers>
                    <DataTrigger TargetType="Label"
                                 Binding="{Binding IsExpanded}"
                                 Value="True">
                        <Setter Property="Text"
                                Value="&#x25BE;" />
                    </DataTrigger>
                </Label.Triggers>
            </Label>
            <Label Margin="0, 12, 0, 6"
                   Text="{Binding Key}"
                   Grid.Column="1"
                   TextColor="{Binding Converter={StaticResource GroupKeyToColorConv}}"
                   FontSize="Medium"
                   HorizontalOptions="Start" />
        </Grid>
    </DataTemplate>
</ContentPage.Resources>

Here's the result at runtime

Support

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.

Regards,
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.
Tags
ListView
Asked by
Joshua
Top achievements
Rank 2
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or