How to implements Recursive mode with Independent mode of checkbox mode with tree view

1 Answer 17 Views
TreeView
Hanoch
Top achievements
Rank 1
Hanoch asked on 15 Apr 2025, 08:30 AM | edited on 15 Apr 2025, 09:41 AM

Hi community,

We wait a long time for recursive mode feature in tree view.

But we found it has some limitations, when parent is expanded and selected and It's childs are selected .

No option to uncheck disable childs.

The support said the need to implement  Recursive mode with Independent mode and add my logic inside.

I added a basic sample code and would like to add the following logic , can you help to implement these scenarios:

Scenario 1 -When the parent expanded and parent checked and  have children, all it's children need to be checked :

You can check whether the child items are nodes from the parent and add them to the Checked Items collection.

Scenario 2 -When the parent not expanded and parent checked and have no children: 

Just add this item to the Checked Items collection. 

Scenario 3 -When  I uncheck child the parent should be unchecked:

How is can be done ?

Scenario 4 - When parent unchecked, how i unchecked all its children:

How is can be done ?

Thanks in advance,

1 Answer, 1 is accepted

Sort by
0
Didi
Telerik team
answered on 15 Apr 2025, 10:46 AM

Hello Dani,

Following from the support ticket you have opened here are some details you can use to achieve the custom scenario you have: 

Scenario 1 -when the folder expanded and folder checked and  have children: - this is implemented here https://docs.telerik.com/devtools/maui/knowledge-base/treeview-net-maui-load-children-checkbox-recursive  Change the mode to independent, the control works the same way. The child items are added to the CheckedItems collection. Here is the logic:

private void LoadChildNodes(TreeNode parentNode)
{
	for (int itemIndex = 0; itemIndex < nodesCount; itemIndex++)
	{
		var childNode = this.CreateTreeNode();
		childNode.Parent = parentNode;
		parentNode.Children.Add(childNode);

		if (parentNode.IsChecked == true)
		{
			this.checkedTreeNodes.Add(childNode);
		}
	}
}

 

Scenario 4 - When parent unchecked, how i unchecked all its children: Same scenario as 1, just revert when IsChecked is false:

private void LoadChildNodes(TreeNode parentNode)
{
	for (int itemIndex = 0; itemIndex < nodesCount; itemIndex++)
	{
		var childNode = this.CreateTreeNode();
		childNode.Parent = parentNode;
		parentNode.Children.Add(childNode);

		if (parentNode.IsChecked == false)
		{
			this.checkedTreeNodes.Remove(childNode);
		}
	}
}

 

Scenario 2 -When the folder not expanded and folder checked and have no children: 

You can use the ItemChecked event and add additional logic to add items to the CheckedItems collection:

private void treeView_ItemChecked(object sender, Telerik.Maui.Controls.TreeView.TreeViewItemViewInteractionEventArgs e)
{

	var item = e.Item as Category;
	if (item.Children == null) 
	{
		this.vm.CheckedTreeNodes.Add(item);
	}
}

 

The code for scenario 2 is extended from the example here -> https://docs.telerik.com/devtools/maui/controls/treeview/load-children-on-demand   

Note that if you expand the item, you have to add some additional logic in the command, to check whether the parent node is already added to the CheckedItems collection.

Scenario 3 -When I uncheck child the parent should be unchecked:

Use ItemUnchecked event and implement logic to change the parent item IsChecked state, when child is unchecked or just remove the parent from te CheckedItems collection.

Note that you have to add logic to check whether parent or child is already added to the CheckedItems collection, and skip adding it twice.

Regards,
Didi
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Hanoch
Top achievements
Rank 1
commented on 16 Apr 2025, 08:07 AM | edited

I tried your solution but there are many more senario.

Can anyone from community can give a full solution?

Thanks,

Tags
TreeView
Asked by
Hanoch
Top achievements
Rank 1
Answers by
Didi
Telerik team
Share this question
or