Skip to content

Commit fb97a98

Browse files
Merge pull request #3 from Santhosh-SF4792/main
Update the README.md file
2 parents 7c80edd + bf0eb65 commit fb97a98

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,82 @@
1-
**[View document in Syncfusion Xamarin Knowledge base](https://www.syncfusion.com/kb/12553/how-to-update-checkbox-state-programmatically-in-xamarin-forms-treeview-sftreeview)**
1+
# update-checkbox-state-programmatically-treeview-xamarin
2+
3+
This repository demonstrates how to update the checkbox state of nodes programmatically in the Syncfusion Xamarin.Forms TreeView control. The sample shows how to check or uncheck TreeView nodes in response to application logic, user actions outside the TreeView, or ViewModel changes. It covers how to manipulate the checked state directly from code.
4+
5+
## Sample
6+
7+
### Behaviour
8+
9+
```csharp
10+
public class TreeViewBehavior : Behavior<SfTreeView>
11+
{
12+
SfTreeView TreeView;
13+
14+
protected override void OnAttachedTo(SfTreeView bindable)
15+
{
16+
base.OnAttachedTo(bindable);
17+
18+
TreeView = bindable;
19+
TreeView.Loaded += TreeView_Loaded;
20+
}
21+
22+
private void TreeView_Loaded(object sender, TreeViewLoadedEventArgs e)
23+
{
24+
UpdateCheckBoxState();
25+
TreeView.CheckBoxMode = CheckBoxMode.Recursive;
26+
}
27+
28+
private void UpdateCheckBoxState()
29+
{
30+
var checkedNodes = TreeView.GetCheckedNodes();
31+
foreach (var node in checkedNodes)
32+
{
33+
if (node.ParentNode == null && node.HasChildNodes)
34+
{
35+
//Update child nodes based on parent's checked state.
36+
SetChildChecked(node);
37+
}
38+
39+
else
40+
{
41+
//Update parent node based on child's checked state.
42+
SetParentChecked(node);
43+
}
44+
}
45+
}
46+
47+
private void SetChildChecked(TreeViewNode node)
48+
{
49+
foreach (var child in node.ChildNodes)
50+
{
51+
child.IsChecked = true;
52+
if (child.HasChildNodes)
53+
{
54+
SetChildChecked(child);
55+
}
56+
}
57+
}
58+
59+
private void SetParentChecked(TreeViewNode node)
60+
{
61+
if (node.ParentNode == null) return;
62+
63+
if (node.ParentNode.ChildNodes.All(x => x.IsChecked == true))
64+
node.ParentNode.IsChecked = true;
65+
66+
else node.ParentNode.IsChecked = null;
67+
68+
if(node.ParentNode != null)
69+
{
70+
SetParentChecked(node.ParentNode);
71+
}
72+
}
73+
}
74+
```
75+
76+
## Requirements to run the demo
77+
78+
To run the demo, refer to [System Requirements for Xamarin](https://help.syncfusion.com/xamarin/system-requirements)
79+
80+
## Troubleshooting
81+
### Path too long exception
82+
If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.

0 commit comments

Comments
 (0)