Skip to content

Commit a8b3e1e

Browse files
Update the README.md file
1 parent 7c80edd commit a8b3e1e

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

README.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
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+
Visual Studio 2017 or Visual Studio for Mac.
78+
Xamarin add-ons for Visual Studio (available via the Visual Studio installer).
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.
83+
84+
## License
85+
86+
Syncfusion® has no liability for any damage or consequence that may arise from using or viewing the samples. The samples are for demonstrative purposes. If you choose to use or access the samples, you agree to not hold Syncfusion® liable, in any form, for any damage related to use, for accessing, or viewing the samples. By accessing, viewing, or seeing the samples, you acknowledge and agree Syncfusion®'s samples will not allow you seek injunctive relief in any form for any claim related to the sample. If you do not agree to this, do not view, access, utilize, or otherwise do anything with Syncfusion®'s samples.

0 commit comments

Comments
 (0)