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

Grouping Header format

5 Answers 324 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Julian
Top achievements
Rank 1
Julian asked on 12 Jul 2017, 12:41 PM

Hey guys, 

I am currently implementing a grouping in my Listview. The data is getting grouped by a Datetimevalue.

I can sort the Listview by using this Datetime but then I get the long Datetimevalue inside the header I tryed using stringformat and a valueconverter but without any effect.

Is there a way to modify the output in the groupingheader?

Best regards

Julian

5 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 14 Jul 2017, 03:38 PM | edited on 07 Apr 2022, 12:57 PM
Hello Julian,

You can create a custom GroupHeaderTemplate with a Label to display the text.

<telerikDataControls:RadListView.GroupHeaderTemplate>
    <DataTemplate>
        <Grid>
            <Label Text="{Binding }" />
        </Grid>
    </DataTemplate>
</telerikDataControls:RadListView.GroupHeaderTemplate>


Now that you have you have access to the Label UI Element that renders the text, you can alter the bound text any way you see fit. One option you mentioned is to use a value converter to get the exact string format you want.


Demo

I've written a demo for you, find it attached. Here's a high level look at the relevant parts of the demo:

We convert the value to a string when it's bound to the header text, so you'll need to parse it back into a DateTime object, then return the newly formatted string. 

Here's a converter using the "g" DateTime string format:

class GroupHeaderDisplayFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // the bound header text is a string
        var headerText = value as string;
 
        // get the valid DateTime object (dont use this in production, instead use a TryParse)
        var parsedDateTime = DateTime.Parse(headerText);
 
        // Return the formatted string using your preferred DataTime string format
        return parsedDateTime.ToString("g");
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


Now, in your XAML, use the converter on Label's binding:

<ContentPage.Resources>
    <ResourceDictionary>
        <converters:GroupHeaderDisplayFormatConverter x:Key="DisplayFormatConverter"/>
    </ResourceDictionary>
</ContentPage.Resources>
 
...

<telerikDataControls:RadListView.GroupHeaderTemplate>
    <DataTemplate>
        <Grid BackgroundColor="#C1C1C1">
            <Label Text="{Binding Converter={StaticResource DisplayFormatConverter}}"  />
        </Grid>
    </DataTemplate>
</telerikDataControls:RadListView.GroupHeaderTemplate>


Here's a screenshot of the result at run-time:




Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
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
0
Julian
Top achievements
Rank 1
answered on 25 Jul 2017, 12:31 PM

Thanks for your support!

This was exactly what I was looking for!

Best regards

Julian

0
BYISHIMO
Top achievements
Rank 1
answered on 08 Apr 2019, 10:56 AM
It's very helpful, thanks. But I would suggest to put things like this into the documentation.
0
Lance | Manager Technical Support
Telerik team
answered on 08 Apr 2019, 02:38 PM
Hello,

You can find the documentation in RadListView Grouping (scroll down to the GroupHeaderTemplate section).

I'll talk to the development team to see if there's way to make this easier to find. Right now, we have it in the Grouping topic because that's where we would expect a developer to look for topics related to the grouping feature. However, it might be useful to have it also in the styling section.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
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
0
Bernd
Top achievements
Rank 1
Iron
answered on 07 Apr 2022, 11:17 AM

This is an old question, but I found it when I searched for a solution today.

With the current version of Telerik UI for Xamarin it's possible to use StringFormat instead of a custom converter.

<telerikDataControls:RadListView.GroupHeaderTemplate>
    <DataTemplate>
        <Label Text="{Binding Key, StringFormat='{0:D}'}" />
    </DataTemplate>
</telerikDataControls:RadListView.GroupHeaderTemplate>
The BindingContext of the GroupHeader is a complex object and the Key property contains the group key.
Lance | Manager Technical Support
Telerik team
commented on 07 Apr 2022, 01:00 PM

Hi Bernd,

Most definitely! since StringFormat is now an option available in Xamarin.Forms, it makes modifying the Key value for display easier and cleaner. 

Tags
ListView
Asked by
Julian
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Julian
Top achievements
Rank 1
BYISHIMO
Top achievements
Rank 1
Bernd
Top achievements
Rank 1
Iron
Share this question
or