Programmatically set CheckedItems does not reflect Parent nodes CheckBoxes

Hello,

I am using SfTreeView control in BoundMode with MVVM. In my ViewModel I have ObservableCollection<object> CheckedItems. SfTreeView CheckedItems is bound to the ViewModel's CheckedItems (View: CheckedItems="{Binding CheckedItems}") and at the same time CheckBoxMode is set to Recursive.

In the initialization of the ViewModel I have a logic of "pre-checked" items. I am adding some (not all) ViewModels, that are used as a context for Nodes, into the CheckedItems ObservableCollection. The TreeView renders those Nodes as checked (which is very nice and expected) but their parents are unchecked instead of to be in  intermediate state.

Current state:

Expected state:

Adding Parent ViewModel into CheckedItems ObservableCollection doesn't help, because then those gets checked instead of intermediate:

Please find attached Xamarin.Forms Android sample project where the issue occurs

Any chance of getting workaround or fixing the issue?


Attachment: TreeViewIntermediateCheckBox_6e2006a7.zip

3 Replies 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team April 5, 2021 03:19 AM UTC

Hi Kristian, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Programmatically set CheckedItems does not reflect Parent nodes CheckBoxes” from our side. We would like to inform you that when setting the CheckedItems programmatically the parent nodes state will not be updated. The parent nodes checked state is updated only in UI interaction. This is the expected behavior in SfTreeView. 
 
Please refer to our user guidance document regarding the same, 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan 
 


Marked as answer

KR Kristian April 6, 2021 09:51 AM UTC

Hello Lakshmi,

Thank you for you answer.

Despite the note on the page https://help.syncfusion.com/xamarin/treeview/checkbox#checkbox-state I rather dispute it is a expected behavior from user point of view. Maybe it is intended by the implementation. As the SfTreeView behaves differently in programmatic and UI mode for IsChecked, it makes it a little inconsistent.

However I was able to fix-it via little workaround:
        TreeView.CheckBoxMode = CheckBoxMode.None;
        SetNodesCheckBox(TreeView.Nodes);
        ...
        private void SetNodesCheckBox(TreeViewNodeCollection nodes)
        {
            foreach (var node in nodes)
            {
                if (node.HasChildNodes)
                    SetNodesCheckBox(node.ChildNodes);
                
                if (node.ParentNode == null)
                    break;

                if (node.ParentNode.ChildNodes.All(x => x.IsChecked.GetValueOrDefault(false)))
                {
                    node.ParentNode.IsChecked = true;
                    break;
                }

                if (node.ParentNode.ChildNodes.Any(x => x.IsChecked.GetValueOrDefault(true)))
                    node.ParentNode.IsChecked = null;
                
                if (!node.HasChildNodes)
                    break;
            }
        }
        ...
        TreeView.CheckBoxMode = CheckBoxMode.Recursive;

Of course it would be better to have this working properly out of the box.


LN Lakshmi Natarajan Syncfusion Team April 8, 2021 03:32 AM UTC

Hi Kristian, 
 
Thank you for the update. 
 
We are glad that your requirement has been met at your end. Please let us know if you need any further update. As always we are happy to help you out. 
 
Lakshmi Natarajan 
 


Loader.
Up arrow icon