We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Need to programmatically expand tree node if a child node meets certain criteria

When initially opening my tree view, I want to auto-expand a parent node if a child meets certain criteria.
I tried adding an IsExpanded property to my model class and binding that property to the IsExpanded property in the template style,
but it doesn't like that because my DataContext is set to the viewModel class instead of the model class.

I attached a sample solution based on one you gave me earlier. My application is set up in a similar fashion.
I've commented out the line in the UserControl.xaml that doesn't work.

Hopefully you can show me how to, for example, open up the 'Business Intelligence' node if its 'Silverlight' child met my condition.



Attachment: CHECKE_1847122217_c6988908.zip

5 Replies

SK Senthil Kumaran Rajan Syncfusion Team August 9, 2018 06:26 AM UTC

Hi Steve 
 
Thank you for the update. 
 
In the provided sample, we observe that the value for IsExpanded property has not been assigned, so the items has not been expanded at startup. However, we have assigned the value for IsExpanded property for the items and expand the particular node using some condition . Please download the sample for the same from the following location: 
 
 
Also you can refer the following KB on how to expand the particular node on some condition using Button click from the below KB link: 
 
 
Please try this solution and let us know if it is helpful. 
 
Regards 
Senthil 



SM Steve McWilliams August 9, 2018 02:52 PM UTC

I see that your sample is working to come up with 2 of the nodes expanded and one not expanded.

After changing my IsExpanded setter in XAMl to match yours, I tried just expanding the first node in the tree as a test.
It npw comes up with the arrow highlighted in black, but still not expanded to see the children.
I didn't include the IsSelect-related changes because that seemed just related to your button action.

What did I miss doing in attached modified solution to expand first node?


Attachment: CHECKE_1847122217_c6988908_28a18c87.zip


SK Senthil Kumaran Rajan Syncfusion Team August 10, 2018 06:06 AM UTC

Hi Steve 
 
Thank you for the update. 
 
We hope that your requirement is to expand all the TreeViewItemAdv in startup of application. In our example, we have implemented the TreeViewItemAdv as a each model item, so if you want to expand all item, the IsExpanded property should be enabled for all model items. Please find the code for the same from below: 
 
Code:[C#] 
 
 
var m = treeview.Items[0] as Model; 
           if (m.IsExpanded == false) 
                m.IsExpanded = true; 
            for (int i = 0; i < (m as Model).Models.Count; i++) 
            { 
                if ((m as Model).Models[i].IsExpanded == false) 
                { 
                    (m as Model).Models[i].IsExpanded = true; 
                } 
 
            } 
 
 
The same has been modified in the sample and attached the same for your reference. Please download it from following: 
 
 
Please let us know if it is helpful and meet your requirement. 
 
Regards 
Senthil 



SM Steve McWilliams August 10, 2018 09:26 PM UTC

I did not have a root model in my tree, so Items[0] in my scenario was trying to expand the first item only, not the whole tree.

But, I have realized that my issue was because I was using IsLoadOnDemand=true, and the children were not displaying when I expanded the parent because they were not loaded yet.
I was able to get it to work by loading the children first and then setting IsExpanded=true for the parent node meeting my criteria.

The only thing that I think is not quite right  is that I am not able to get to the TreeViewItemAdv (loadingItem) to set its IsLoadOnDemand to false.
So, the children still will load again the first time user clicks on the arrow.
Maybe you have an idea how to fix that, but at least it is working Ok now.


SK Senthil Kumaran Rajan Syncfusion Team August 13, 2018 06:33 AM UTC

Hi Steve 
 
Thank you for the update. 
 
In TreeViewAdv, IsLoadOnDemand property is used to load the items dynamically when a particular TreeViewItem is expanded. i.e the child node will be loaded once expanding the parent node in TreeViewAdv. This is the default behavior. For more information about the IsLoadOnDemand property, please refer the following UG link: 
 
 
Regards 
Senthil 


Loader.
Up arrow icon