Conditional grouping if value exists

1 Answer 3 Views
CollectionView
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Joe asked on 04 Dec 2025, 01:13 AM

I have CollectionView sometimes I want it to be a flat format without grouping and other times I want it grouped.  Basically depending on how the input data.  I have figured out ways to have the group display nothing, but there is still a space for where the group header would be.

How could I go about this type of setup.  

Basically something like:

if string.IsNullOrEmpty(GroupName)
   //Don't group
else
    //group

1 Answer, 1 is accepted

Sort by
0
Accepted
Didi
Telerik team
answered on 04 Dec 2025, 11:55 AM

Hi Joe,

The grouping is per property name, if you want to have a conditional grouping, then you need to implement a custom logic whether to add or clear the group descriptors if the property is null or empty, here is an example:

private void Button_Clicked(object sender, EventArgs e)
{
	if(this.collectionView.BindingContext as ViewModel is null)
	{
		return;
	}

	if (this.vm.Locations.Any(item => string.IsNullOrEmpty(item.Country)))
	{
		this.collectionView.GroupDescriptors.Clear();
		return;
	}

	this.collectionView.GroupDescriptors.Add(new DelegateGroupDescriptor
	{
		DisplayContent = "Country",
		KeyLookup = new CustomIKeyLookup()
	});
}

and the example code is from here: https://www.telerik.com/maui-ui/documentation/controls/collectionview/grouping/delegate-group-descriptor . It is up to you whether to use the delegate or property group descriptor of the CollectionView.

Regards,
Didi
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Joe
Top achievements
Rank 2
Iron
Iron
Veteran
commented on 05 Dec 2025, 12:06 AM

Thank you!
Tags
CollectionView
Asked by
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Didi
Telerik team
Share this question
or