Completed
Last Updated: 17 Sep 2024 09:33 by ADMIN
Vaibhav
Created on: 09 Jan 2024 13:45
Category: ListView
Type: Feature Request
2
ListView: Expose GroupHeaderTemplate Selector property
 I want to use the TemplateSelector for the group header. 
1 comment
ADMIN
Didi
Posted on: 17 Sep 2024 09:33

Hi Vaibhav,

We have resolved this behavior by implementing a new control, CollectionView, which is a complete rewrite of the ListView from the ground up. CollectionView offers improved performance, enhanced features, and a modernized approach to managing lists of data. The CollectionView incorporates all key features of the ListView.

As this new control supersedes the ListView, this feature request will be closed. We recommend transitioning to CollectionView to take full advantage of its capabilities. Visit the following article that explains how to migrate to the new RadCollectionView.

CollectionView Grouping is described here: https://docs.telerik.com/devtools/maui/controls/collectionview/grouping/overview 

If you want to apply template selector, here are more details: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/datatemplate?view=net-maui-8.0#create-a-datatemplateselector 

Here is an example:

public class GroupDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate ValidTemplate { get; set; }
    public DataTemplate InvalidTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        var data = item as GroupContext;
        if(data!=null && data.Key == "Austria")
        {
            return ValidTemplate;
        }
        return InvalidTemplate;
    }
}
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="validPersonTemplate">
                <Grid>
                    <Label TextColor="Black" VerticalTextAlignment="Center">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="Country: " />
                                <Span Text="{Binding Key}" TextColor="#00796B" FontAttributes="Bold" />
                                <Span Text=", Cities: " />
                                <Span Text="{Binding Items.Count}" TextColor="#00796B" FontAttributes="Bold" />
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                </Grid>
            </DataTemplate>
            <DataTemplate x:Key="invalidPersonTemplate">
                <Grid>
                    <Label TextColor="Yellow" VerticalTextAlignment="Center">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="Country: " />
                                <Span Text="{Binding Key}" TextColor="Blue" FontAttributes="Bold" />
                                <Span Text=", Cities: " />
                                <Span Text="{Binding Items.Count}" TextColor="Red" FontAttributes="Bold" />
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                </Grid>
            </DataTemplate>
            <local:GroupDataTemplateSelector x:Key="personDataTemplateSelector"
                                          ValidTemplate="{StaticResource validPersonTemplate}"
                                          InvalidTemplate="{StaticResource invalidPersonTemplate}" />

        </ResourceDictionary>
    </ContentPage.Resources>
    <telerik:RadCollectionView ItemsSource="{Binding Locations}"
                               GroupHeaderTemplate="{StaticResource personDataTemplateSelector}"
                           DisplayMemberPath="City">
        <telerik:RadCollectionView.BindingContext>
            <local:ViewModel />
        </telerik:RadCollectionView.BindingContext>
        <telerik:RadCollectionView.GroupDescriptors>
            <telerik:PropertyGroupDescriptor PropertyName="Country" />
        </telerik:RadCollectionView.GroupDescriptors>
    </telerik:RadCollectionView>

 

Regards,
Didi
Progress Telerik